You need to create a data frame that contains the mean of mpg by cyl
and pass that to geom_vline(). Said data frame requires the faceting
variable cyl and the xintercept aesthetic maps to the mean value. I'd
also suggest taking the aesthetic mapping out of ggplot() and place it
in geom_histogram() instead. Something like
library(plyr)
library(ggplot2)
avgmpg <- plyr::ddply(mtcars, .(cyl), summarise, mean_mpg = mean(mpg))
ggplot(mtcars) +
geom_histogram(aes(x = mpg), fill = "darkorange") +
geom_vline(data = avgmpg, aes(xintercept = mean_mpg), size = 1.2) +
facet_wrap(~ cyl)
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2
>
> --- You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
ggplot2+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.