Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding individual lines to facet_wrapped plot

I'm trying to use facet_wrap to plot a bunch of dataset as scatterpoints, each with an individual line in them indicating when a specific event happened. However, I haven't been able to get the lines to show up individually in each plot, but rather they all show up in all of the plots. After melting, the data looks like:

names(data) = c("Date","ID", "event_date", "variable", "value")

where I want each plot to be a scatter plot of value ~ Date, and each plot divided up by ID with a vertical line appearing at each "event_date" that shows up for each ID. My best efforts have gotten me to:

p <- qplot(Date, value, data=dat, colour=variable)
p <- p + geom_vline(xintercept=as.numeric(dat$event_date))
p + facet_wrap(~ID)

Which works perfectly except for all of the vertical lines showing up in every subplot. Any suggestions? Reading through the documentation hasn't gotten me anywhere yet.

like image 893
Austin Avatar asked Aug 05 '11 22:08

Austin


1 Answers

ggplot(dat, aes(Date,value))+ geom_point() + geom_vline(data=dat,aes(xintercept=as.numeric(event_date))) + facet_wrap(~ID)

Is how I do the same thing using facet_grid(). I'm pretty sure it will work for facet_wrap as well.

like image 191
wespiserA Avatar answered Sep 24 '22 01:09

wespiserA