rggplot2facet-wraperrorbar

How can I remove extra error bars in ggplot?


enter image description here

Does anyone know why suddenly my error bars are showing up in each column instead of their designated one? It was working before, but my x-axis was in the wrong order, showing "0" "16" "32" "8". I changed Well to be a factor with:

welldata$Well_f <- factor(welldata$Well, levels = c('0', '8', '16', '32'))

but once I did that it changed my error bars to go everywhere instead of only in their corresponding well. Can anyone help me fix this without reversing my facet_wrap order?

ggplot(welldata, aes(x = E2F4, y = Colonies, color = Clone, fill = E2F4)) +
  geom_point(size = 2) +
  geom_errorbar(data = error_data, aes(ymin = ymin, ymax = ymax), width = 0.2, color 
    = "black") +
  labs(title = "HT29 Colony Survival",
      x = "Concentration of SN-38 (nM)",
      y = "Number of colonies") +
      scale_color_manual(values = c('11' = "darkturquoise", "7" = "darkturquoise", 
      "2" = "darkturquoise","Control"= "darkorchid")) +
   scale_fill_discrete(labels = c("-/- = E2F4 Knockout", "+/+ = E2F4 Wild Type")) +
   guides(color = guide_legend(override.aes = list(size = 3)), fill = 
   guide_legend(override.aes = list(size = 2))) +
 facet_wrap(~Well_f, nrow = 1, strip.position = "bottom") +
   theme_minimal() +
   theme(strip.placement = "outside",
   panel.spacing = unit(0, "cm"),
   strip.text = element_text(size = 10, margin = margin(b = 15)),
   legend.title = element_text(size = 10),
   plot.title = element_text(margin = margin(b = 10)))

When I write "Well" in facet_wrap in goes back to the correct error bars but the ordering gets messed up again. Put it back as "Well_f" and the ordering works but all the extra error bars come back.

data:

structure(list(E2F4 = c("+/+", "+/+", "+/+", "-/-", "-/-", "-/-", 
"-/-", "-/-", "-/-", "-/-", "-/-", "-/-", "+/+", "+/+", "+/+", 
"+/+", "+/+", "+/+", "-/-", "-/-", "-/-", "-/-", "-/-", "-/-", 
"-/-", "-/-", "-/-", "+/+", "+/+", "+/+"), Colonies = c(1052, 
983, 1057, 497, 464, 437, 111, 81, 85, 40, 42, 43, 523, 523, 
636, 646, 730, 749, 58, 60, 53, 705, 746, 785, 54, 82, 80, 1618, 
1470, 1505), Clone = c("Control", "Control", "Control", "7", 
"7", "7", "11", "11", "11", "2", "2", "2", "Control", "Control", 
"Control", "Control", "Control", "Control", "2", "2", "2", "7", 
"7", "7", "11", "11", "11", "Control", "Control", "Control"), 
Well = c("32", "32", "32", "32", "32", "32", "32", "32", 
"32", "32", "32", "32", "32", "32", "32", "32", "32", "32", 
"32", "32", "32", "32", "32", "32", "32", "32", "32", "32", 
"32", "32"), Hour = c(24, 24, 24, 24, 24, 24, 24, 24, 24, 
24, 24, 24, 24, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 
48, 48, 48, 48, 48, 48), Well_f = structure(c(4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", 
"8", "16", "32"), class = "factor")), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"))

error data:

error_data <- data.frame(
E2F4 = c("-/-", "+/+", "-/-", "+/+", "-/-", "+/+", "-/-", "+/+"),
Colonies = c(2837.3333, 1911.33333, 137, 259.6667, 95.666, 
557.6667, 74.333, 757.3333),
Well = c("0", "0", "8", "8", "16", "16", "32", "32"),
Errors = c(334.02993877795, 103.568010, 158.745, 197.616630, 
93.0931, 133.82949, 66.515, 43.98106))

error_data <- error_data %>%
mutate(ymin = Colonies - Errors, ymax = Colonies + Errors)

Solution

  • Although it is not directly asked, you may also want to see this post. I think using facet_wrap is not the best option.

    To solve your problem, I will add a line in your error data that matches the facet_wrap parameter you used for welldata.

    error_data <- error_data %>%
      mutate(ymin = Colonies - Errors, ymax = Colonies + Errors) %>% 
      mutate(Well_f = factor(Well, levels = c('0', '8', '16', '32'))) ## added here