Jack
Jack

Reputation: 857

How to solve the following error in geom_errorbar: "requires the following missing aesthetics: x or xmin and xmax"?

I create first a graph that shows the estimates of the coefficients of my outcome y. It works fine. Here is the code:

ggplot(pred, aes(y=y, fill= x2)) + 
  geom_col(aes(x1), position = "dodge") 

However, when i try to add the geom_error bars as shown below, i get the following Error: geom_errorbar requires the following missing aesthetics: x or xmin and xmax. I am not sure what i am missing, can someone help?

ggplot(pred, aes(y=y, fill= x2)) + 
  geom_col(aes(x1), position = "dodge") +
  geom_errorbar(aes(ymin=y-sd, ymax=y+sd), width=.2,position=position_dodge(.9)) 

Here is the data:

structure(list(y = c(0.164302489928412, 0.215532635370365, 0.164302489928412, 
0.131435285839938), x1 = structure(c(1L, 1L, 2L, 2L), .Label = c("0", 
"1"), class = "factor"), x2 = structure(c(1L, 2L, 1L, 2L), .Label = c("0", 
"1"), class = "factor"), sd = c(0.0148510153999744, 0.0141698339939684, 
0.0148510153999744, 0.0250010070315256)), row.names = c(NA, 4L
), class = "data.frame")

Upvotes: 0

Views: 5589

Answers (1)

Annette
Annette

Reputation: 38

You just need to put the name/variable you used for the x-values in your data (in your case x1 first: geom_errorbar(aes(x1, ymin = y - sd, ymax = y + sd), width=.2,position=position_dodge(.9)). Worked perfectly fine for me.

Upvotes: 2

Related Questions