Search code examples
rggplot2patchwork

How to specify tagging levels in R PATCHWORK package having specified layout using a character vector?


Using patchwork, you can specify the layout of plots using a character vector and the design argument as bellow:

layout <- "

AABBBBDD

AACCCCDD

"

plot_layout(design = layout, guides = "collect") 

I want tag the plots "A1","A2","B","C". However, the following code tags, "A1", "B", "C","D"

patchwork <- p2 + p1 + p3 + p4 + 
  plot_layout(design = layout, guides = "collect")

patchwork[[1]] <- patchwork[[1]] + theme_minimal() + plot_layout(tag_level = "new")
patchwork + Annots

I have tried puting brackets around (p2 +p1) to indicate that they should be nested together, but this hasn't helped.

Any help on this issue would be appreciated!

ps the plots used were produced as follows:

p1 <- ggplot(mpg) + 
   geom_point(aes(x = displ, y = hwy))`

p2 <- ggplot(mpg) + 
  geom_bar(aes(x = as.character(year), fill = drv), position = "dodge") + 
  labs(x = "year")

p3 <- ggplot(mpg) + 
  geom_density(aes(x = hwy, fill = drv), colour = NA) + 
  facet_grid(rows = vars(drv))

p4 <- ggplot(mpg) + 
  stat_summary(aes(x = drv, y = hwy, fill = drv), geom = "col", fun.data = mean_se) +
  stat_summary(aes(x = drv, y = hwy), geom = "errorbar", fun.data = mean_se, width = 0.5)```




Solution

  • You can simply do:

    patchwork <- p2 + p1 + p3 + p4 + 
      plot_layout(design = layout, guides = "collect")
    
    patchwork + plot_annotation(tag_levels = list(c("A1", "A2", "B", "C")))
    

    enter image description here