patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Nested graph and tag alignment

Open hliebert opened this issue 2 years ago • 0 comments

In the following case, how can I get the second tag/panel title correctly centered (i.e. like the first one)? I tried using titles and that didn't work (lower title was getting cut off). plot.tag = element_text(hjust = 0.5) doesn't seem to have any effect here.

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

ptop <- p1
pbot <- p1 + p2 + p3 + p4
combined <- ptop + pbot +
  plot_layout(ncol = 1, guides = "collect") +
  plot_annotation(tag_levels = list(c("(a) Panel a", "(b) Panel b "))) &
  theme(plot.tag = element_text(hjust = 0.5, size = 30),
        plot.tag.position = "top",
        legend.position = "bottom") 
combined

Screenshot from 2021-10-20 12-36-46

hliebert avatar Oct 22 '21 13:10 hliebert