My code that makes custom annotations in faceted histograms in ggplot2 3.1.1 fails on a different computer running ggplot2 3.2.1, throws this error: Error: Aesthetics must be either length 1 or the same as the data (9): label
How can I make my annotations appear in the facets made in ggplot2 3.2.1?
Minimal example using mtcars follows.
Thank you!!
library(ggplot2, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
lb <- mtcars %>%
group_by(cyl) %>%
summarize(n=n(), avg_gear=round(mean(gear),1))
lb$label <- paste0("n=",lb$n, " avg_gear=",lb$avg_gear)
print(lb)
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
annotate("text", x=4, y=13, label=lb$label) +
ggtitle("histograms of gear, faceted by cyl")
You might use another layer:
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
geom_text(data = lb, aes(x = 4, y = 13, label = label)) + # in place of annotate
ggtitle("histograms of gear, faceted by cyl")