Search code examples
rggplot2plotmarginfacet

How to increase the space between facet boxes and plot edges?


I am using facet_grid to create a title in my plot that is surrounded by a black box with white background colour. I wonder how I could increase the space between this box with the title and the upper edge of my plot. Below I show a similar plot and I give a fake code to play with: enter image description here

library(ggplot2)
library(cowplot)
library(ggpubr)
theme_set(theme_cowplot())

df1 <- data.frame(x = 1:12, y = (1:12)^2, grp=c('A', 'B', 'C'), depth=c("5m","5m","5m","5m"))

p1 <- ggplot(df1, aes(x, y, color=grp)) + geom_point() + facet_grid(.~depth) + 
  theme(strip.background = element_rect(colour = "black", fill = "white", size = 1.5, linetype = "solid"))
p1

Does anyone know how to do it? An alternative to increase space between the facet box and the upper edge of the plot could be also to do not use facet_grid. However, I couldn't find an alternative way to draw a title with a black box surrounding. If you have other way instead of using facet_grid is welcome.


Solution

  • Using @Dekike link:

    ggplot(df1, aes(x, y, color=grp)) + 
      geom_point() + 
      facet_grid(.~depth) + 
      theme(strip.background.x = element_rect(colour = "black", fill = "white", size = 1.5, linetype = "solid"),
            strip.placement = "outside",
            strip.switch.pad.grid = unit(0.2, "in"))
    

    enter image description here