Scott Chamberlain
2010-Nov-09 16:41 UTC
[R] ggplot2: facet_grid with different vertical lines on each facet
Hello, I am plotting many histograms together using facet_grid in ggplot2. However, I want to then add a vertical line to each histogram, or facet, each of which vertical lines are at different x-values. The following example adds all vertical lines to each facet: ggplot(data,aes(values)) + geom_histogram() + facet_grid(.~variable) + geom_vline(xintercept=c(5,10,15)) How can I add a vertical line at different x positions on each facet? Thanks very much, Scott Chamberlain [[alternative HTML version deleted]]
Ista Zahn
2010-Nov-09 18:11 UTC
[R] ggplot2: facet_grid with different vertical lines on each facet
Hi Scott, You can put the vline data in a separate data.frame: dat <- data.frame(x=rnorm(20), y=rnorm(20), z=rep(c("a", "b"), each=10)) vline.dat <- data.frame(z=levels(dat$z), vl=c(0,1)) library(ggplot2) ggplot(dat, aes(x=x, y=y)) + geom_point() + geom_vline(aes(xintercept=vl), data=vline.dat) + facet_grid(.~z) Best, Ista On Tue, Nov 9, 2010 at 4:41 PM, Scott Chamberlain <schamber at rice.edu> wrote:> Hello, > > I am plotting many histograms together using facet_grid in ggplot2. However, > I want to then add a vertical line to each histogram, or facet, each of > which vertical lines are at different x-values. > > The following example adds all vertical lines to each facet: > ggplot(data,aes(values)) + geom_histogram() + facet_grid(.~variable) + > geom_vline(xintercept=c(5,10,15)) > > How can I add a vertical line at different x positions on each facet? > > Thanks very much, > Scott Chamberlain > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org