为什么错误栏位于图(组)的中心?

Why is the errorbar placed at the center of the plot (group)?

我想在我的情节中添加 errorbar。错误栏没有为每个 bar/col 放置。所有错误栏都放在 group 的中央。情节看起来像

我有的代码

df <- transform(
  Result_1,
  group = reorder(interaction(Result_Name, Model_Name, Dataset_Number),
                  -Result_Mean)
)

levels(df$Dataset_Number)[match("dataset_1",levels(df$Dataset_Number))] <- "Dataset_1"
levels(df$Dataset_Number)[match("dataset_2",levels(df$Dataset_Number))] <- "Dataset_2"

plot_com <- ggplot(df) + 
  geom_col(
    aes(x = Result_Name,
        y = Result_Mean, 
        fill= Model_Name,
        group = group), 
    position=position_dodge(width=1)
  ) + 
  geom_errorbar(
    aes(x = Result_Name,ymin=Result_Mean-Result_sd, ymax=Result_Mean+Result_sd), 
    width=.2,                    # Width of the error bars
    position=position_dodge(.9))+

  facet_grid( ~ Dataset_Number,
              labeller = labeller(Dataset_Number = function(x)paste0(" ",x))) + 
  ggtitle("Percentage of Result") +
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Result")+
  ylab("Result Value")

预期输出

  1. 每个bar/col都需要误差线。
  2. 如果我不想根据 Result Value 重新排序栏,那么我必须做什么?也就是说,Model_Name的位置默认是固定的(不需要用Result_Value重新排列)。

可重现的数据

Result_1 <- structure(list(Model_Name = c("M_1", "M_1", "M_1", "M_1", "M_1", 
"M_1", "M_2", "M_2", "M_2", "M_2", "M_2", "M_2", "M_3", "M_3", 
"M_3", "M_3", "M_3", "M_3", "M_5", "M_5", "M_5", "M_5", "M_5", 
"M_5", "M_4", "M_4", "M_4", "M_4", "M_4", "M_4", "M_6", "M_6", 
"M_6", "M_6", "M_6", "M_6", "M_7", "M_7", "M_7", "M_7", "M_7", 
"M_7", "M_8", "M_8", "M_8", "M_8", "M_8", "M_8"), Dataset_Number = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("dataset_1", 
"dataset_2"), class = "factor"), Result_Name = c("Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3", "Result_1", 
"Result_2", "Result_3", "Result_1", "Result_2", "Result_3"), 
    Result_Mean = c(56.2825438620055, 35.6378424218613, 17.4284724341723, 
    66.6442239087732, 36.8592532979545, 15.9738121683694, 52.7325534968645, 
    33.1746900931854, 17.1757392425061, 64.6535342402335, 35.800341437544, 
    15.7253225028994, 39.5479141226974, 30.9550233556993, 17.3476420027052, 
    59.4138479126739, 35.0144576861987, 15.9230629844388, 39.5479141226974, 
    30.9550233556993, 17.3476420027052, 59.4138479126739, 35.0144576861987, 
    15.9230629844388, 57.5402174648061, 41.6685970371373, 17.2942924490357, 
    31.3552087504645, 39.2537436373919, 15.904978122944, 63.6320734135852, 
    39.5005999891646, 17.4328632135356, 70.882815479768, 38.587835397129, 
    15.9738121683694, 83.6930861233382, 60.4172142037466, 39.5331576314268, 
    22.5230231229633, 15.1509806996773, 9.06560167264891, 5.11163337250294, 
    3.25980177679993, 2.25437625433477, 0.110342848845767, 0.145325630169788, 
    0.086710964578538), Result_sd = c(1.60530512109249, 1.08728309030594, 
    0.605894566069689, 0.930828089504724, 0.490115202879507, 
    0.199504674029123, 1.48389734600799, 1.06924934395385, 0.439534402691696, 
    0.889887988854293, 0.541301806217695, 0.197779898953328, 
    1.63347517285651, 0.485420897674829, 0.555136470391021, 0.681334694767627, 
    0.362244937017463, 0.202516350391391, 1.63347517285651, 0.485420897674829, 
    0.555136470391021, 0.681334694767627, 0.362244937017463, 
    0.202516350391391, 1.96753965756147, 1.23960313293375, 0.646620888123093, 
    1.18730114529279, 0.49648969857036, 0.201243691395992, 1.68110682564507, 
    1.43134355059874, 0.612605993579786, 0.903672723329879, 0.523353969587384, 
    0.199504674029123, 6.3026995179373, 5.2848636398466, 2.53065263183837, 
    1.82894252597019, 1.52505871922224, 0.883972961967627, 5.59105212339757, 
    3.09202665660649, 1.37145910054188, 0.169074977761456, 0.137804194536823, 
    0.0444745905757077)), row.names = c(NA, -48L), class = c("tbl_df", 
"tbl", "data.frame"))

预期输出 1:只需将 aec 放在 ggplot 而不是 geom_col 中。在这里更改您的代码

plot_com <- ggplot(df, aes(x = Result_Name,
                           y = Result_Mean,
                           fill= Model_Name,
                           group = group)) + 
  geom_col(
    position=position_dodge(width=1)
  )

预期输出 2:如果您不想重新排列条形图,请删除 transform 函数。

df <- transform(
...
)

使用你的 Result_1 df 直接到 ggplot。最重要的是,从 plot_com 函数中删除所有 group

注意:如果您不想重新排列栏,您还需要从我的 Expected Output 1 中删除 group

一码通

plot_com <- ggplot(Result_1, aes(x = Result_Name,
                           y = Result_Mean,
                           fill= Model_Name)) + 
  geom_col(
    position=position_dodge(width=1)
  ) + 
  geom_errorbar(
    aes(x = Result_Name,ymin=Result_Mean-Result_sd, ymax=Result_Mean+Result_sd), 
    width=.2,                    # Width of the error bars
    position=position_dodge(.9))+

  facet_grid( ~ Dataset_Number,
              labeller = labeller(Dataset_Number = function(x)paste0(" ",x))) + 
  ggtitle("Percentage of Result") +
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("Result")+
  ylab("Result Value")