这里有一个数据集

df=data.frame(
  cultivar = rep(c("CV1", "CV2"), each = 8L),
  part = rep(rep(c("DW1", "DW2"), 2), each = 4L),
  Light = rep(rep(c("Light1", "Light2"), 4), each = 2L),
  Treatment = rep(c("Treatment 1", "Treatment 2"), 8),
  DW_Mean = c(69.2825, 94.82, 57.8025, 90.7225, 26.6975, 37.3, 21.73, 28.3525, 58.825,
    79.45, 45.3225, 67.2425, 24.78, 32.2425, 16.8475, 24.9825
  ),
  DW_se = c(7.3890441138666, 12.3060777666972, 4.7905851678057, 5.91931355113637,
    5.10530830769961, 2.13571221531991, 1.87420205243014, 0.906838601957373,
    3.60502773914432, 7.59180918270913, 2.066044429177, 4.76078840634616,
    2.56271990926307, 1.23644904868741, 1.94433096891107, 2.04306295791393))

我想在每个堆叠的条上加上误差条.所以我用了这个代码.

   library(dplyr)
   library(ggplot2)

   dplyr::mutate(df, .by=cultivar, cume_y=cumsum(DW_Mean)) |>
        ggplot(aes(x=cultivar, y=DW_Mean, fill=part)) +
          geom_bar(stat="identity", position=position_stack(reverse=T), width=0.7, size=1) +
          geom_errorbar(aes(ymin=cume_y-DW_se, ymax=cume_y+DW_se),
                        position="identity", width=0.3) +
          scale_fill_manual(values = c("coral4", "grey45")) +
          scale_y_continuous(breaks = seq(0, 200, 50), limits = c(0, 200)) +
          facet_grid(~ Light ~ Treatment, scales = "free") +
          annotate("segment", x = 1, xend = 2, y = Inf, yend = Inf, color = "black", lwd = 1) +
          annotate("segment", y = 50, yend = 150, x = Inf, xend = Inf, color = "black", lwd = 1) +
          theme_classic(base_size = 18, base_family = "serif") +
          theme(legend.position = c(0.4, 0.9),
          legend.title = element_blank(),
          axis.line = element_line(linewidth = 0.5, colour = "black"),
          strip.background = element_rect(color = "white", linewidth = 0.5, linetype = "solid"))

现在,误差条放错地方了.如果我过滤每个变量(即,只有处理1和光1)并创建堆叠条,我可以将错误条放置在堆叠条的确切位置.然而,当我使用facet_grid()时,错误条放错地方了.

你能告诉我如何解决这个问题吗?

谢谢,

enter image description here

推荐答案

我认为你的geom_bar()geom_errorbar()代码应该工作正常.但是,生成cume_y变量的代码可能有问题.我将.by=cultivar替换为.by = c(Treatment, Light, cultivar),如下所示

library(dplyr)
library(ggplot2)

df |>
  mutate(.by = c(Treatment, Light, cultivar), cume_y = cumsum(DW_Mean)) |>
  ggplot(aes(x = cultivar, y = DW_Mean, fill = part)) +
  geom_bar(stat = "identity", position = position_stack(reverse = T), width = 0.7, size = 1) +
  geom_errorbar(aes(ymin = cume_y - DW_se, ymax = cume_y + DW_se),
    position = "identity", width = 0.3
  ) +
  scale_fill_manual(values = c("coral4", "grey45")) +
  scale_y_continuous(breaks = seq(0, 200, 50), limits = c(0, 200)) +
  facet_grid(~Light ~ Treatment, scales = "free") +
  annotate("segment", x = 1, xend = 2, y = Inf, yend = Inf, color = "black", lwd = 1) +
  annotate("segment", y = 50, yend = 150, x = Inf, xend = Inf, color = "black", lwd = 1) +
  theme_classic(base_size = 18, base_family = "serif") +
  theme(
    legend.position = c(0.4, 0.9),
    legend.title = element_blank(),
    axis.line = element_line(linewidth = 0.5, colour = "black"),
    strip.background = element_rect(color = "white", linewidth = 0.5, linetype = "solid")
  )

enter image description here

R相关问答推荐

将一个载体的值相加,直到达到另一个载体的值

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

如何使用geom_sf在边界显示两种 colored颜色 ?

寻找图片边缘

如果第一个列表中的元素等于第二个列表的元素,则替换为第三个列表的元素

在GGPLATE中将突出的点放在前面

可以替代与NSE一起使用的‘any_of()’吗?

如何从容器函数中提取conf并添加到ggplot2中?

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

我如何go 掉盒子图底部的数字?

如果COLSUM为>;0,则COLNAME为向量

使用未知字符数(不受限制的最大长度)的Lookback有什么好的替代方案?

在R中使用列表(作为tibble列)进行向量化?

在不对R中的变量分组的情况下取两行的平均值

如果条件匹配,则使用Mariate粘贴列名

R基于变量组合创建新的指标列

对R中的列表列执行ROW Mean操作

在REST API中使用参数R

将R中对象的CSV数组转换为JSON数组

对数据帧中的列进行子集设置以通过迭代创建新的数据帧