3

I am working with probability data in a barplot. The barplot looks fine, but when I try to add the errorbars they are not organized or bound to the appropriate column. It'd be perfect if I didn't have to do this manually. (This example only shows a subset.)

Can you help me figure out how to add the errorbars in the right location?

This is my code:

data[complete.cases(data),] %>% 
  ggplot(aes(x = Var.name, y = Prob_mean, fill = Response)) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_errorbar(aes(ymin = lower, ymax = Prob_mean), stat = "identity", 
                position = position_dodge(width = 0.9), width = 0.3) 

Picture of the graph:

position_dodge

This is my data:

> dput(data)
structure(list(Var.name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L), .Label = c("Biomass", "Bacteria.total", 
"Gpos", "Gneg"), class = "factor"), Response = c("Decrease", 
"Increase", "Neutral", "Decrease", "Increase", "Neutral", "Decrease", 
"Increase", "Neutral", "Decrease", "Increase", "Neutral"), Prob_mean = c(0.267825247615756, 
0.589316466289715, 0.142858286094529, 0.272971629090396, 0.575649507723523, 
0.09076335727541, 0.392857191685997, 0.357142750965404, 0.250000057348599, 
0.499952457038418, 0.392844812870964, 0.107202730090619), Prob_n = c(56L, 
56L, 56L, 33L, 33L, 33L, 28L, 28L, 28L, 28L, 28L, 28L), Prob_se = c(0.023672823211199, 
0.0344812896356437, 0.0143539351411692, 0.0319407298087323, 0.0477306098555942, 
0.0300122568287155, 0.0553692531148468, 0.036801084975757, 0.0609410527844315, 
0.0252493129246497, 0.0320001555212106, 0.0344986116155648), 
    lower = c(0.244152424404557, 0.554835176654071, 0.12850435095336, 
    0.241030899281664, 0.527918897867928, 0.0607511004466945, 
    0.33748793857115, 0.320341665989647, 0.189059004564168, 0.474703144113768, 
    0.360844657349753, 0.0727041184750539), upper = c(0.291498070826955, 
    0.623797755925359, 0.157212221235698, 0.304912358899128, 
    0.623380117579117, 0.120775614104125, 0.448226444800843, 
    0.393943835941161, 0.310941110133031, 0.525201769963067, 
    0.424844968392174, 0.141701341706184)), .Names = c("Var.name", 
"Response", "Prob_mean", "Prob_n", "Prob_se", "lower", "upper"
), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))

EDIT:

I would like to have the errorbars stacked, but when I use "stack" this happens: stacked
and the error: Stacking not well defined when not anchored on the axis. I would like to have lower errorbars as stacked position instead of dodge.

My question is different from How to stack error bars in a stacked bar plot using geom_errorbar?, because it uses the "stack" function and my problem with the errorbars is when using the "fill" function.

robinCTS
  • 5,746
  • 14
  • 30
  • 37
Sarah
  • 31
  • 5
  • 1
    @Sarah, have you seen this post on [stacked error bars](https://stackoverflow.com/questions/30872977/how-to-stack-error-bars-in-a-stacked-histogram-using-geom-errorbar-ggplot) – mnm Feb 14 '18 at 11:00
  • You just have to remove `position_dodge` from your errorbar argument! – erocoar Feb 14 '18 at 11:01
  • @Ashish Thank you so much, the post you shared got me in the right direction! – Sarah Feb 14 '18 at 13:53
  • @Erocoar thank you! Sadly this did not solve the issue. Luckily I found another solution with some puzzling! – Sarah Feb 14 '18 at 13:55
  • 1
    @Sarah please consider posting the solution and accepting it – guy mograbi Feb 15 '18 at 00:13

1 Answers1

0

I almost fully solved the issue. Nevertheless, I had to go back to my data to exclude another response option that was not shown in the graph. This influenced my errorbars, because they were correct as "stack" and not as "fill" (as the data scaled to the "fill" option). When I removed that data and ran the analysis again, the errorbars were perfect.

This was my solution:

data <- data %>% 
  mutate(vadj = ifelse(Response == "Neutral", 0, Prob_mean))

neutral.val = data %>% 
  filter(Response == "Neutral")

rep(neutral.val$Prob_mean, each=3)

incr.val = data %>% 
  filter(Response == "Increase")

rep(incr.val$Prob_mean, each=3)

data <- data %>% 
  mutate(Incr.val =rep(incr.val$Prob_mean, each=3), Neutral.val = rep(neutral.val$Prob_mean, each=3)) %>% 
  mutate(vadj = ifelse(Response == "Neutral", 0, 
                       ifelse(Response == "Increase", Neutral.val, Neutral.val+Incr.val)))

data%>% 
  ggplot(aes(x = Var.name, y = Prob_mean, fill = Response)) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_errorbar(aes(ymin = lower+vadj, ymax = Prob_mean+vadj), stat = "identity", #position = "stack",
                width = 0.3)
Sarah
  • 31
  • 5