Closed hummuscience closed 1 year ago
This is a hard to debug issue with operator precedence. The plot_annotation()
is added to the (p4/p5)
rather than to the full plot because |
has lower precedence than +
. As plot_annotation()
(for now) only have an effect on the top level patchwork it seems to vanish.
You can get the correct look by enclosing the whole assembly in parentheses before adding annotation:
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg))
p4 <- ggplot(mtcars) +
geom_bar(aes(gear)) +
facet_wrap(~cyl)
p5 <- ggplot(mtcars) +
geom_bar(aes(vs)) +
facet_wrap(~cyl)
(p1 |
(p2 / p3) |
(p4 / p5)) +
plot_annotation(tag_levels = 'A')
Created on 2023-08-09 by the reprex package (v2.0.1)
I went with the example form the documentation.
The below works
This doesn't