简体   繁体   English

将误差线添加到不同层中的点-ggplot

[英]Add error bars to points in different layers -ggplot

I am trying to add confidence intervals to the points displayed in the attached plot using ggplot2. 我正在尝试使用ggplot2将置信区间添加到附件图中显示的点上。 (2 datasets, one X axis, 2 Y axes) (2个数据集,一个X轴,两个Y轴)

The points represent the mean value (val column in the dataframe). 这些点表示平均值(数据框中的val列)。 The data frame also contains the upper and lower values (L and U columns) 数据框还包含上限值和下限值(L和U列)

t<-data.frame(rep(c( "AA","AG","GG"),2), c(79.29365,47.67004,22.86688,28.41990,33.29651,36.76852),
           c(71.18651,45.97425,22.04625,27.23392,32.97892,36.57194),
           c(87.40079,49.36582,23.68751,29.60588,33.61411,36.96510),
           c(rep("a",3),c(rep("b",3))))
colnames(t)<-c("x","val","L","U","panel")

d1<-t[1:3,]
d2<-t[4:6,]

p <- ggplot(data = t, mapping = aes(x =t$x, y = t$val))
p <- p + facet_grid(panel ~ ., scale = "free")
p <- p + layer(data = d1, mapping=aes(x = d1$x, y = d1$val), geom = c( "point"), stat = "identity",size=3) 
p <- p + layer(data = d2, mapping =aes(x = d2$x, y = d2$val),geom = "point", stat = "identity",size=3)
p<-p+theme_bw()
p<-p+xlab("Genotypes")+ylab("Quantitative trait")+ggtitle("RS")
p<-p+scale_color_hue()
p

在此处输入图片说明

I have managed to plot the error bars in a simple ggplot graph for one dataset, however I am failing to correctly add them to both plots as I have tried to do above. 我已经设法在一个简单的ggplot图中为一个数据集绘制了误差线,但是如上所述,我无法正确地将误差线添加到两个图中。

ggplot(d1, aes(x = d1$x, y = d1$val)) +  geom_point(colour="gray", size = 4)+geom_errorbar(aes(ymax = d1$U, ymin = d1$L),width=0.05) +theme_bw()

Thanks in advance. 提前致谢。

Just add the following command to the existing plot and everything will work fine: 只需将以下命令添加到现有绘图中,一切将正常运行:

geom_errorbar(aes(ymax = U, ymin = L), width = 0.05) 

Note. 注意。 I refer to U and L instead of d1$U and d1$L . 我指的是UL而不是d1$Ud1$L The errorbar limits of both layers are already present in t . t中已经存在两层的误差线限制。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM