When I facet a plot I often want to point out interesting comparisons between groups. For instance, in the plot produced by this code I'd like to point out that the second and third columns are nearly identical.
library(tidyverse)
ggplot(mtcars, aes(x = as.factor(am), y = mpg)) +
stat_summary(fun.y = "mean", geom = "col") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .1) +
facet_grid(~ vs)
Currently I can only make this annotation by exporting my plot to another app like Preview or Powerpoint and manually adding the lines and text across facets.
My efforts to add an annotation across facets results in annotations that do not leave their own facet. See below.
ggplot(mtcars, aes(x = as.factor(am), y = mpg)) +
stat_summary(fun.y = "mean", geom = "col") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .1) +
facet_grid(~ vs) +
annotate("errorbarh", xmin = 2, xmax = 3, y = 25, height = .5,
color = "red") +
annotate("text", x = 2.5, y = 27, label = "NS", color = "red")
Any advice about how to extend lines and annotations across facets would be greatly appreciated.