1

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)

mean distance +/- SE, by group at each time point

AB.Tplot <-qplot(starttime, 0, data = subset(tdists, Group1=="rowA" & Group2=="rowH" & value<0.05))

timepoints at which p-value for rowA and rowH distances is < 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)

working plot

Community
  • 1
  • 1
dnagirl
  • 20,196
  • 13
  • 80
  • 123
  • You unmapped `colour` in `geom_ribbon` to avoid the same problem. You need to do it in `geom_point` as well. – joran Apr 03 '14 at 18:04
  • @joran: actually, setting `colour=NULL` in `geom_ribbon` was just to get rid of the ribbon outlines. I didn't know it was solving another issue too. I just found this [answer](http://stackoverflow.com/questions/11714951/remove-extra-legends-in-ggplot2?rq=1) about removing extra legends which made me *finally* understand that mapping and setting could be 2 separate operations. – dnagirl Apr 03 '14 at 18:22

1 Answers1

2

This answer is somewhat speculative, since I only have a tiny portion of your data to play with.

But I think it's time you stopped using qplot.

The reason you got that error is that aesthetic mappings are inherited between layers in ggplot2. So the aesthetics you mapped inside qplot are passed down to each subsequent layer, unless they are redefined or explicitly unmapped.

For this reason, when building up plots with lots of layers, using different data sets, using the more explicit ggplot notation is frequently much clearer.

I think this is more like what you're after:

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, alpha=0.1, fill=mapped, colour=NULL)) +
    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.8), 
               size=1)

Your original qplot call should have essentially included a geom_line layer, so I'm not sure why you added another geom_line call. Perhaps it was covered up by the ribbon. Again, explicitly building up the layers starting with ggplot() makes this much clearer.

joran
  • 169,992
  • 32
  • 429
  • 468