简体   繁体   English

Geom_violin + geom_error_bar

[英]Geom_violin + geom_error_bar

I have 500 estimations of 3 objects. 我有3个对象的500个估计。 My goal is to plot a violin plot to understand the distribution of the estimation, but at the same time, I want to visualize the standard error (evaluated in another way), the mean value estimate and the true value. 我的目标是绘制一个小提琴图,以了解估计的分布,但同时,我想可视化标准误差(以另一种方式评估),均值估计和真实值。

This is what I have: 这就是我所拥有的:

object1 <- rnorm(500,mean=1,sd=0.1)
object2 <- rnorm(500,mean=2,sd=0.1)
object3 <- rnorm(500,mean=3,sd=0.1)

estimations <- data.frame(object1,object2,object3)
colnames(estimations) <- 1:3

SEframe <- data.frame()
SEframe <- rbind(SEframe,c(1,1,mean(object1),0.1))
SEframe <- rbind(SEframe,c(2,2,mean(object2),0.15))
SEframe <- rbind(SEframe,c(3,3,mean(object3),0.25))

colnames(SEframe) <- c("ID","True.value","Estimated.value","SE")

estMelted <- melt(estimations)
estMelted$variable <- as.factor(estMelted$variable)

p <- ggplot(estMelted, aes(x=variable, y=value)) + 
  geom_violin()

Now I'd like to have on the graph, a line for the true value and an errorbar for the estimation and the SE. 现在,我想在图表上有一条线表示真实值,并用一条误差线表示估算值和SE。

How I can do it? 我该怎么办?

You can always specify another data sets for the additional layers. 您始终可以为其他图层指定另一个数据集。 Here, we add a geom_errorbar layer and a geom_point layer, both of which we use with data=SEframe . 在这里,我们添加了geom_errorbar层和geom_point层,我们都将它们与data=SEframe

p + 
  geom_errorbar(data=SEframe, aes(x=ID, 
             ymin=Estimated.value - SE, 
             ymax=Estimated.value+SE), inherit.aes=F) + 
  geom_point(data=SEframe, aes(x=ID, y=Estimated.value))

Note the usage of inherit.aes=FALSE . 请注意inherit.aes=FALSE的用法。 The reason is as follows: by default, geom_errorbar will inherit the mapping from ggplot() , but that mapping uses a column named value . 原因如下:默认情况下,geom_errorbar将继承ggplot()的映射,但是该映射使用名为value的列。 Even though geom_errorbar does not need it (because it does not need y ), it will still be inherited and cause problems. 即使geom_errorbar不需要它(因为它不需要y ),它仍然会被继承并引起问题。 Thus, we specify that aes should not be inherited. 因此,我们指定aes不应该被继承。

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

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