0

I have 500 estimations of 3 objects. 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.

How I can do it?

Cath
  • 23,906
  • 5
  • 52
  • 86
TheAvenger
  • 458
  • 1
  • 6
  • 19
  • 1
    Just add the geom_errorbar specifying that you need separate data. `p + geom_errorbar(data=SEframe, aes(x=ID, y=Estimated.value, ymin=Estimated.value - SE, ymax=Estimated.value+SE)) + geom_point(data=SEframe, aes(x=ID, y=Estimated.value))` – January Jul 17 '19 at 11:48

1 Answers1

1

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.

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. The reason is as follows: by default, geom_errorbar will inherit the mapping from ggplot(), but that mapping uses a column named value. Even though geom_errorbar does not need it (because it does not need y), it will still be inherited and cause problems. Thus, we specify that aes should not be inherited.

January
  • 16,320
  • 6
  • 52
  • 74