Is it possible to create a geom_errorbar scatterplot in ggplot with confidence intervals for both x and y directions?

Viewed 38

I currently have the following dataset. I have two sets of means, each with their 2*standard errors:

dd <- data.frame(m1 = c(3,7,5,4), m2 = c(5,3,6,1), se1 = c(1.1,1.3,1.1,1.9), se2 = c(1.2,1.22,1.54,1.7))

ggplot(dd, aes(x = m1, y = m2, ymin = m2-se2, ymax = m2+se2, xmin = m1-se1, xmax = m1+se2))+ 
      geom_errorbar(aes(color = m1), width = 0.2)


> dd
  m1 m2 se1  se2
1  3  5 1.1 1.20
2  7  3 1.3 1.22
3  5  6 1.1 1.54
4  4  1 1.9 1.70

This is the plot I get:

enter image description here

This only shows the confidence intervals for m2, using se2. Is there a way to show the confidence intervals for m1 too? I.e., I used line creation to get:

ggplot(dd, aes(x = m1, y = m2, ymin = m2-se2, ymax = m2+se2, xmin = m1-se1, xmax = m1+se2))+ 
  geom_segment(aes(x=m1-se1,y=m2,xend=m1+se1,yend=m2))+
  geom_errorbar(aes(color = m1), width = 0.2)

but am wondering if there is a way to do this with ggplot2 in order to get the interval ends as well.

enter image description here

1 Answers

You can plot two error bars and flip the orientation of the second error bar, e.g.

library(tidyverse)

dd <- data.frame(m1 = c(3,7,5,4), m2 = c(5,3,6,1), se1 = c(1.1,1.3,1.1,1.9), se2 = c(1.2,1.22,1.54,1.7))

ggplot(dd, aes(x = m1, y = m2, color = m1))+ 
  geom_errorbar(aes(ymin = m2-se2, ymax = m2+se2), width = 0.2) +
  geom_errorbar(aes(xmin = m1-se1, xmax = m1+se1), orientation = "y")

example_2.png

Related