Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2条形图中组织堆叠的错误条_R_Ggplot2_Probability - Fatal编程技术网

R 在ggplot2条形图中组织堆叠的错误条

R 在ggplot2条形图中组织堆叠的错误条,r,ggplot2,probability,R,Ggplot2,Probability,我正在处理条形图中的概率数据。条形图看起来不错,但当我尝试添加errorbars时,它们没有组织或绑定到相应的列。如果我不需要手动操作,那就太完美了。(此示例仅显示一个子集。) 你能帮我找出如何在正确的位置添加错误条吗 这是我的代码: data[complete.cases(data),] %>% ggplot(aes(x = Var.name, y = Prob_mean, fill = Response)) + geom_bar(stat = "identity", pos

我正在处理条形图中的概率数据。条形图看起来不错,但当我尝试添加errorbars时,它们没有组织或绑定到相应的列。如果我不需要手动操作,那就太完美了。(此示例仅显示一个子集。)

你能帮我找出如何在正确的位置添加错误条吗

这是我的代码:

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) 
图表的图片:

这是我的数据:

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

编辑:

我希望将错误条堆叠起来,但当我使用“堆叠”时,会发生以下情况:
错误是:未在轴上定位时,堆叠没有很好地定义。我希望有较低的错误栏作为堆叠位置,而不是道奇


我的问题不同于,因为它使用了“stack”函数,而错误条的问题是在使用“fill”函数时。

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

这就是我的解决方案:

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)
数据%
变异(vadj=ifelse(响应==“中性”,0,概率平均值))
neutral.val=数据%>%
过滤器(响应=“中性”)
代表(中性值$Prob_平均值,每个=3)
增量值=数据%>%
过滤器(响应=“增加”)
代表(增量值$Prob_平均值,每个=3)
数据%
突变(Incr.val=rep(Incr.val$Prob_平均值,每个=3),Neutral.val=rep(Neutral.val$Prob_平均值,每个=3))%>%
变异(vadj=ifelse(响应==“中性”,0,
ifelse(响应==“增加”,Neutral.val,Neutral.val+Incr.val)))
数据%>%
ggplot(aes(x=Var.name,y=Prob_平均值,fill=Response))+
几何图形栏(stat=“identity”,position=“fill”)+
geom_errorbar(aes(ymin=lower+vadj,ymax=Prob_mean+vadj),stat=“identity”,#position=“stack”,
宽度=0.3)

@Sarah,你看过这篇文章了吗?你只需要从你的errorbar参数中删除
位置\u dodge
@Ashish非常感谢你,你分享的帖子让我找到了正确的方向@谢谢你!遗憾的是,这并没有解决问题。幸运的是,我找到了另一个令人费解的解决方案@莎拉:请考虑发布解决方案并接受它。