在ggplot2 barplot中组织堆叠的错误栏

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

我正在处理条形图中的概率数据。条形图看起来很好,但是当我尝试添加错误栏时,它们没有被组织或绑定到适当的列。如果我不必手动执行此操作,那将是完美的。 (此示例仅显示子集。)

你能帮我弄清楚如何在正确的位置添加误差线吗?

这是我的代码:

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) 

图表图片:

position_dodge

这是我的数据:

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

编辑:

我希望堆叠错误栏,但当我使用“堆栈”时,会发生这种情况:stacked 和错误:未锚定在轴上时堆叠不明确。我希望将较低的错误栏作为堆叠位置而不是闪避。

我的问题与How to stack error bars in a stacked bar plot using geom_errorbar?不同,因为它使用“堆栈”功能,而错误栏的问题在于使用“填充”功能。

r ggplot2 probability
1个回答
0
投票

我几乎完全解决了这个问题。然而,我不得不回到我的数据,以排除图中未显示的另一个响应选项。这影响了我的错误栏,因为它们是正确的“堆栈”而不是“填充”(因为数据缩放为“填充”选项)。当我删除该数据并再次运行分析时,错误栏是完美的。

这是我的解决方案:

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