Using these 2 data.frames,
ds.SG
> head(subset(ds.SG, mapped %in% c("rowA", "rowH")))
starttime mapped meandist se
1 0 rowA 126.2125 9.094259
8 0 rowH 113.3708 9.552690
9 60 rowA 134.4000 10.693561
16 60 rowH 115.8542 9.618504
17 120 rowA 148.9458 10.630781
24 120 rowH 124.8958 12.446691
tdists
> head(subset(tdists, Group1=="rowA" & Group2=="rowH" & value<0.05))
starttime Group2 Group1 value vs
259 540 rowH rowA 0.0273469043 rowA.rowH
287 600 rowH rowA 0.0032981182 rowA.rowH
315 660 rowH rowA 0.0170252864 rowA.rowH
343 720 rowH rowA 0.0195995924 rowA.rowH
371 780 rowH rowA 0.0047677680 rowA.rowH
399 840 rowH rowA 0.0004149648 rowA.rowH
I can create the following 2 plots:
AB.distplot <-qplot(starttime, meandist, data=subset(ds.SG, mapped %in% c("rowA", "rowH")),
geom="line",colour=mapped, alpha=1)
+ geom_ribbon(aes(ymin=meandist-se, ymax=meandist+se, alpha=0.1, fill=mapped, colour=NULL))
+ geom_line(size=1)
AB.Tplot <-qplot(starttime, 0, data = subset(tdists, Group1=="rowA" & Group2=="rowH" & value<0.05))
I want to combine them such that AB.Tplot
is overplotted on the bottom of AB.distplot
Following an idea I found here, I tried this:
AB.distplot + geom_point(
data = subset(tdists, Group1=="rowA" & Group2=="rowH" & value < 0.5),
aes(starttime, 0),
position = position_jitter(height=0.8),
size=1)
#Error in eval(expr, envir, enclos) : object 'mapped' not found
Since 'mapped' is only in ds.SG
, I don't expect to find it in tdists
(used in the geom_point
). Why is ggplot
looking for it and how can I overplot the way I'd like?
Solution
Since @Joram didn't have all my data, he couldn't see exactly how the plot would turn out. Following his excellent explanation, this is what I did and what I got:
tdists$y <- 0
ggplot(data = subset(ds.SG, mapped %in% c("rowA", "rowH")),aes(x = starttime,y = meandist)) +
geom_ribbon(aes(ymin=meandist-se, ymax=meandist+se, fill=mapped), alpha=0.1) +
geom_line(aes(colour = mapped),alpha = 1,size = 1) +
geom_point(data = subset(tdists, Group1=="rowA" & Group2=="rowH" & value < 0.5),
aes(y = y),
position = position_jitter(height=0),
size=1)