1

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.

Dekike
  • 1,264
  • 6
  • 17
  • 1
    maybe useful; https://stackoverflow.com/questions/56153261/distancing-facet-grid-strips-from-the-faceted-canvas – user20650 May 26 '20 at 13:10
  • dekike, I've closed this as a dup as the link gives theaccepted answer – user20650 May 26 '20 at 15:05
  • I think this post might be useful since there can be people who can't find the answer with the other post. I was looking for a while before I decided to make my own question. I see that the question in the post you shared is quite unexpected for inexperienced people like me, and hence, difficult to find. But it is up to you. Thanks for sharing the post! – Dekike May 26 '20 at 15:13
  • dekike; agreed re the language in your question may be helpful. By marking as a dup, now when users search using similar language to your question they will be directed to the other question; so your question is still useful / helpful.(this question has not been deleted or removed from the search path) – user20650 May 26 '20 at 15:15
  • Am!! Ok ok!! It is perfect then! Thanks! – Dekike May 26 '20 at 15:28
  • @user20650, I did this post because I couldn't find anything about it: https://stackoverflow.com/questions/62029503/how-to-increase-space-among-different-boxes-created-for-the-facet-labels-using . It is about how to increase space among different facet boxes. If you know about some post of if you directly know how to do it, I would appreciate your help! – Dekike May 26 '20 at 19:06

1 Answers1

1

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

Jrm_FRL
  • 1,394
  • 5
  • 15