I am trying to do something similar to this question, but hoping to do it in a single block rather than caching values separately.
I am creating a chart similar to this:
library(tidyverse)
mtcars %>%
rownames_to_column("carmodel") %>%
mutate(brand = substr(carmodel, 1, 4)) %>%
group_by(brand, cyl) %>%
summarize(avgmpg = mean(mpg)) %>%
ggplot(aes(x=brand, y = avgmpg)) +
geom_point() +
facet_grid(cyl~., scales = "free_y") +
coord_flip()
Grouping by one of the fields, and then charting a calculated summarize()
value on the result, using facets to place similar observations together. What I would like to do is add a line in each facet showing the mean of the observations for that facet. I have tried adding geom_hline(aes(yintercept = mean(avgmpg)))
to the definition, but it returns the mean for the entire dataset, not the observations in the facet:
What I'm after is more like this. (The lines here are drawn with an image editor.)
Is this possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…