Austin
Austin

Reputation: 103

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.

Upvotes: 6

Views: 4953

Answers (1)

wespiserA
wespiserA

Reputation: 3168

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.

Upvotes: 4

Related Questions