adding a vertical line with a different intercept to each facet of a histogram in ggplot2

3,226 views
Skip to first unread message

Bonnie Dixon

unread,
Jul 27, 2013, 8:14:56 PM7/27/13
to davi...@googlegroups.com
Dear D-RUG folks;

I could use help with a graphic I would like to make.  

I made a set of faceted histograms showing the distributions of four groups of observations:

(TST.hist <- ggplot(Hunger, aes(x = TSThrs, group = BedtimeHunger)) + geom_histogram()) + facet_wrap(~ BedtimeHunger, nrow = 4) + theme_bw()

Next, I would like to add a line to each facet showing the mean for that group of observations.  I tried this:

(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


Bonnie M. Dixon, MCP
Doctoral Candidate in Nutritional Biology

Foods for Health Institute
1221 RMI South
University of California, Davis
One Shields Avenue, Davis, CA 95616

Rplot.pdf
Rplot2.pdf

Noam Ross

unread,
Jul 27, 2013, 8:50:49 PM7/27/13
to davi...@googlegroups.com

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.
 
 

Bonnie Dixon

unread,
Jul 27, 2013, 9:23:37 PM7/27/13
to davi...@googlegroups.com
That worked.  Thanks, Noam!  Good to know about that online documentation for ggplot too.

Bonnie
Faceted histograms of TST at each hunger level with means.pdf
Reply all
Reply to author
Forward
0 new messages