satower
satower

Reputation: 13

add error bars to two plots that are overlapped (ggplot2)

I'm having a problem with a plot made with two different data frames. What I need is to graph the error bars, which come from different data sets.

The first data frame:

dput(resudospobl)

structure(list(Poblacion = 
structure(c(1L, 1L, 2L, 2L), .Label = 
c("Carolina", 
"Lavalle"), class = "factor"), sexo = 
structure(c(1L, 2L, 1L, 
2L), .Label = c("hembra", "macho"), 
class = "factor"), N = c(960, 
960, 640, 640), IR = 
c(0.627976287904167, 
0.591445531573958, 
0.752046236173437, 0.748963332945312), 
sd = c(0.241332559805011, 
0.24103347180023, 0.194890181966294, 
0.20467196068143), se = 
c(0.00778897487518677, 
0.00777932185134039, 0.007703710857727, 
0.00809036961157182), 
ci = c(0.0152854016885121, 
0.0152664582011643, 0.0151276489859234, 
0.0158869243550959)), row.names = c(NA, 
-4L), class = "data.frame")

Second data frame:

dput(resudospobl2)

structure(list(Poblacion = 
structure(c(1L, 1L, 2L, 2L), .Label 
= c("Carolina", 
"Lavalle"), class = "factor"), Trat 
= structure(c(1L, 2L, 1L, 
2L), .Label = c("manzana", "uva"), 
class = "factor"), N = c(960, 
960, 640, 640), IR = 
c(0.658423422891667, 
0.560998396586458, 
0.758180928170312, 
0.742828640948437), sd = 
c(0.21174136546939, 
0.259656138696281, 
0.20285509360085, 
0.196492580813269), se = 
c(0.00683392318471805, 
0.00838036584092683, 
0.00801855163431666, 
0.00776705123368289
), ci = c(0.0134111693336726, 
0.0164459714183095, 
0.0157458965866789, 
0.0152520294295554)), row.names = 
c(NA, -4L), class = "data.frame")

where se is standard error. My attempt is:

 ggplot()+
  #first layer
  geom_point(data=resudospobl, 
  aes(x=Poblacion, y=IR, colour=sexo) , 
  shape="square", size= 3)+
  #second layer
  geom_point(data=resudospobl2, 
  aes(x=Poblacion, y=IR, colour=Trat), 
  size=3)+
  #error bars
  geom_errorbar(data=resudospobl , aes( 
  ymin=IR-se, ymax=IR+se), size=0.3, 
  width=.1)+
  geom_errorbar(data=resudospobl2 , aes( 
  ymin=IR-se, ymax=IR+se), size=0.3, 
  width=.1)

The result is an error message. The plot without the error bars is the following, with two populations grouped by sex and treatment:

theres two populations, grouped by sex and treatment

Thanks in advance.

Upvotes: 0

Views: 1252

Answers (1)

aosmith
aosmith

Reputation: 36076

The key part of the error message is object 'x' not found.

You didn't define an x variable globally in ggplot(). Since you also didn't pass one to geom_errorbar(), ggplot can't plot the error bars because it doesn't know where to put them on the x axis.

Your options are to put x in each error bar layer:

ggplot()+
     geom_point(data=resudospobl, 
                aes(x=Poblacion, y=IR, colour=sexo) , 
                shape="square", size= 3)+
     geom_point(data=resudospobl2, 
                aes(x=Poblacion, y=IR, colour=Trat), 
                size=3)+
     geom_errorbar(data=resudospobl , aes(x = Poblacion,
          ymin=IR-se, ymax=IR+se), size=0.3, 
          width=.1)+
     geom_errorbar(data=resudospobl2 , aes(x = Poblacion,
          ymin=IR-se, ymax=IR+se), size=0.3, 
          width=.1)

OR, because you use the same x, y, ymin, and ymax variables for all layers for both datasets you can set these globally in ggplot() instead of separately in each layer.

ggplot(mapping = aes(x=Poblacion, y=IR, ymin=IR-se, ymax=IR+se))+
     geom_point(data=resudospobl, 
                aes(colour=sexo) , 
                shape="square", size= 3)+
     geom_point(data=resudospobl2, 
                aes(colour=Trat), 
                size=3)+
     geom_errorbar(data=resudospobl, size=0.3, 
                   width=.1)+
     geom_errorbar(data=resudospobl2, size=0.3, 
                   width=.1)

Upvotes: 1

Related Questions