Search code examples
rggplot2facet-grid

Remove space at bottom of facet


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.sstatic.net/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()
        )

Solution

  • 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