library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg$hwy))
I want each geom_hline()
in the facet shown above to be the mean of the points that are only contained within that facet. I would think that I could do it with something like (below). But that doesn't work. I'm close, right?
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg %>% group_by(year, fl)$hwy))
If you have the value you wish to use for each facet as a column in the data frame, and that value is unique within each facet, then you can use geom_hline(aes(yintercept=column)), which will then plot a horizontal line for each of the facets
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With