Search code examples
rggplot2geom-col

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


I wanted to add the errorbar in my plot. The errorbar is not placing for each bar/col. All errorbar is placing in the central of the group. The plot look likes

Generated Plot

The code I have

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

Expected Output

  1. Need the error bar in each bar/col.
  2. If I do not want to reorder the bar based on the Result Value then what I have to do? Say, the position of the Model_Name is fixed by default (not need to rearrange with the Result_Value).

Reproducible Data

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

Solution

  • Expected Output 1: Just place the aec in ggplot instead of geom_col. Change your code in here

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

    Expected Output 2: If you do not want to rearrange the bar, remove the transform function.

    df <- transform(
    ...
    )
    

    Use your Result_1 df directly to the ggplot. Most importantly, remove all group from your plot_com function.

    Note: You also need to remove group from my Expected Output 1 if you do not want to rearrange the bar.

    All in one code

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

    enter image description here