How to tag some plots

Hi,

Is there a workaround to add plot tags (as in "A", "B", "C") when using patchwork, in which only some of the plots get a tag? IN my case, it is a 2X2 grid, and I want the labels " A" and "B" on the top left and top right plots

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')

# This tags all of the plots
(p1 | p2 ) /
(p3 | p4) +
 plot_annotation(tag_levels = "A")

# This does not tag anyone

(p1 | p2 +
    plot_annotation(tag_levels = "A")) /
  (p3 | p4) 

# This one gives you an error

(p1 | p2 )+
    plot_annotation(tag_levels = "A") /
  (p3 | p4) 

Hi @rgallego,

The easiest way is probably to just add a tag to each plot that you desire to be tagged. I actually think {patchwork} uses these tags behind the scenes anway.

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  labs(tag = "A") +
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  labs(tag = "B") +
  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')

(p1 | p2 ) /
  (p3 | p4)

Created on 2020-07-16 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.