从不同的数据框添加条纹图 [英] add stripplot from different data.frame
问题描述
使用这两个data.frames,
ds.SG
>头部(子集(ds.SG,在%c(rowA,rowH))中映射%))
starttime映射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
> (子集(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
我可以创建以下2个图:
AB.distplot <-qplot(starttime,meandist,data = subset(ds.SG,%c(rowA, ),
geom =line,color = mapped,alpha = 1)
+ geom_ribbon(aes(ymin = meandist -se,ymax = meandist + se,alpha = 0.1,fill =颜色= NULL))
+ geom_line(size = 1)
AB.Tplot< -qplot(starttime,0,data = subset(tdists,Group1 == rowA& Group2 ==rowH& )
我想将它们组合,使得 AB.Tplot
会在 AB.distplot
的底部重叠绘图。根据一个想法,我发现此处 ,我试过这个:
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)
#eror错误(expr,envir,enclos):找不到对象'mapped'
因为'映射'仅在 ds.SG
中,所以我不希望在 tdists
中找到它(用于该 geom_point
)。为什么 ggplot
正在寻找它,以及我如何以我想要的方式进行重叠绘图?
解决方案
由于@Joram没有我所有的数据,他无法确切知道情节会如何变化。在他的优秀解释之后,这就是我所做的和我得到的:
tdists $ y < - 0
ggplot(data = subset(ds.SG,%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(color = 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)
解决方案这个答案有些臆测,因为我只有一个小小的部分数据可以使用。
但我认为您应该停止使用 qplot
。
你得到那个错误的原因是我审美映射在 ggplot2 中的图层之间继承。因此,您映射到 qplot
中的美学内容会传递到每个后续图层,除非它们被重新定义或显式取消映射。
因此,当使用不同的数据集建立大量图层的图时,使用更明确的 ggplot
表示法通常会更清晰。
我认为这更像你之后的事情:
ggplot
geom_ribbon(aes(ymin = meandist-se,ymax = meandist + se,alpha = 0.1,fill = mapped,color = NULL))+
geom_line(aes(color = 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)
你的原创 qplot
调用本应包含一个 geom_line
图层,所以我不确定为什么要添加另一个 geom_line
呼叫。也许它被丝带遮住了。再次,明确地建立以 ggplot()
开头的图层使得这更清晰。
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)
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.
这篇关于从不同的数据框添加条纹图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!