(TST.hist <- ggplot(Hunger, aes(x = TSThrs, group = BedtimeHunger)) + geom_histogram()) + facet_wrap(~ BedtimeHunger, nrow = 4) + theme_bw() + geom_vline(xintercept = mean(Hunger$TSThrs, na.rm = TRUE), col = "red")
But it drew a line at the grand mean, not the mean of each group (as shown in the attachment).
Then I tried this:
(TST.hist <- ggplot(Hunger, aes(x = TSThrs, group = BedtimeHunger)) + geom_histogram() + theme_bw() + geom_vline(data=aggregate(Hunger$TSThrs, list(Hunger$BedtimeHunger), mean, na.rm = TRUE), mapping=aes(xintercept = x), color="red")) + facet_wrap(~ BedtimeHunger, nrow = 4)
But it drew four lines on each facet showing the means of all four groups (as shown in the second attachment).
Does anyone have an idea for how to get one line on each facet showing the mean for that group?
Thanks a bunch for your help!
Bonnie
There's an example of this in the online documentation at http://docs.ggplot2.org/0.9.3.1/geom_vline.html. You need to create a second data.frame holding the means and organized by the facet variable. So, using plyr::ddply:
library(plyr)
TST.hist <- ggplot(Hunger, aes(x = TSThrs, group = BedtimeHunger)) + geom_histogram()) + facet_wrap(~ BedtimeHunger, nrow = 4) + theme_bw()
TST.hist.vlinedata <- ddply(Hunger, "BedtimeHunger", summarize, BedMean=mean(TSThrs))
TST.hist <- TST.hist + geom_vline(aes(xintercept=BedMean), TST.hist.vlinedata, col="red")
--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/groups/opt_out.