如何删除 ggplot 中的额外误差线?

问题描述 投票:0回答:1

有谁知道为什么我的误差线突然出现在每一列而不是指定的误差线中?它之前可以工作,但我的 x 轴顺序错误,显示“0”“16”“32”“8”。我把 Well 改成了一个因素:

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

但是一旦我这样做了,它就改变了我的误差线,使其可以无处不在,而不仅仅是在相应的井中。任何人都可以帮我解决这个问题而不颠倒我的facet_wrap顺序吗?

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)))

当我在facet_wrap中写下“Well”时,会返回到正确的错误栏,但顺序再次混乱。将其放回“Well_f”并且排序有效,但所有额外的误差线都会回来。

数据:

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 <- 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)
r ggplot2 facet-wrap errorbar
1个回答
0
投票

虽然不是直接询问,但你可能也想看看这个post。我认为使用facet_wrap不是最好的选择。

为了解决您的问题,我将在您的错误数据中添加一行,该行与您用于

facet_wrap
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
© www.soinside.com 2019 - 2024. All rights reserved.