-1

I'm working on a chart with #ggplot and facet but I'm not getting to the result I wish for. With the code shown below I always get some space between labels and bars

Even adding switch="y" I can move the facet titles on the left but the space is still there even using axis.ticks = element_blank().

Here the results I'm stick with https://i.stack.imgur.com/uQV6f.jpg

EDIT Thanks to @StéphaneLaurent I added the scale_y_continuous(expand = c(0,0)) parameter solving the gap problem, what I would do now is replace label with facet and viceversa

df=data.frame(
    CHANNEL=c("IN","IN","IN","OUT","OUT","OUT"),
    AGEING=c("A","B","C","A","B","C"),  
DELTA=c(4.84904880066170385,4.44191343963553464,3.32480818414322288,1.74081237911025144,1.86749666518452639,1.74672489082969418)
)
ggplot(df, aes(AGEING, DELTA, fill=CHANNEL)) +
    geom_bar(stat="identity") + coord_flip() +
    facet_grid(vars(CHANNEL), space = "free", switch="y") +
    theme(legend.position = "none",
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()
        )
Bruno T
  • 13
  • 4
  • Is it what you mean: https://stackoverflow.com/questions/20220424/ggplot2-bar-plot-no-space-between-bottom-of-geom-and-x-axis-keep-space-above ? – Stéphane Laurent Jul 01 '19 at 11:24
  • Thanks @StéphaneLaurent, the expand solved part of the problem removing the space between labels but I still don't know how to swap facet and labels – Bruno T Jul 01 '19 at 11:31
  • Please add more detail to your question. "I'm not getting to the result I wish for" doesn't make it very clear what you want. "I still don't know how to swap facet and labels" implies this was part of your question, but I don't see it. – Jon Spring Jul 01 '19 at 12:11

1 Answers1

1

On top of the expand option mentioned in the comments for your first issue, you can place the facet labels on the outside with theme(strip.placement = "outside"):

ggplot(df, aes(AGEING, DELTA, fill=CHANNEL)) +
  geom_bar(stat="identity") + coord_flip() +
  facet_grid(vars(CHANNEL), space = "free", switch="y") +
  scale_y_continuous(expand = c(0,0)) +
  theme(legend.position = "none",
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()
  ) +
  theme(strip.placement = "outside")

Resulting in:enter image description here

Sven
  • 1,203
  • 1
  • 5
  • 14