ggplot2 - Bars non-aligned to error bars

99封情书 提交于 2020-05-23 10:39:11

问题


I am trying to plot error bars over bars in R using ggplot2. The position of the bars is ok, however the error bars are misaligned, overlapped. Please take a look at the example below:

library(ggplot2)

df = structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L), .Label = c("Stage1", "Stage2", "Stage3", "Stage4"), class = "factor"), 
    scenario = c("A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B", "A", "A", "A", "B", "B", 
    "B"), Period = c("2011-2040", "2041-2070", "2071-2100", 
    "2011-2040", "2041-2070", "2071-2100", "2011-2040", "2041-2070", 
    "2071-2100", "2011-2040", "2041-2070", "2071-2100", "2011-2040", 
    "2041-2070", "2071-2100", "2011-2040", "2041-2070", "2071-2100", 
    "2011-2040", "2041-2070", "2071-2100", "2011-2040", "2041-2070", 
    "2071-2100"), value = c(-1, -2, -3, -1, -3, -4, -2, -1, -1, 
    -2, -1, -1, -5, -6, -6, -5, -6, -7, -1, -1, -1, -1, -1, -1
    ), error = c(0.861817443828852, 0.948522930371594, 0.916295430216828, 
    1.09513465405609, 0.967022228830058, 0.962082007787887, 0.327433241996297, 
    0.296270630257992, 0.241564623986049, 0.324799025797241, 
    0.287300168855865, 0.321141699334506, 0.448402583575847, 
    0.329720917851603, 0.296149789779728, 0.423049544646902, 
    0.320807147599275, 0.286926161100617, 0.46555636533774, 0.494769057825624, 
    0.498445009296978, 0.490846868961772, 0.478102139172148, 
    0.47821442683311), ymin = c(-1.86181744382885, -2.94852293037159, 
    -3.91629543021683, -2.09513465405609, -3.96702222883006, 
    -4.96208200778789, -2.3274332419963, -1.29627063025799, -1.24156462398605, 
    -2.32479902579724, -1.28730016885586, -1.32114169933451, 
    -5.44840258357585, -6.3297209178516, -6.29614978977973, -5.4230495446469, 
    -6.32080714759928, -7.28692616110062, -1.46555636533774, 
    -1.49476905782562, -1.49844500929698, -1.49084686896177, 
    -1.47810213917215, -1.47821442683311), ymax = c(-0.138182556171148, 
    -1.05147706962841, -2.08370456978317, 0.0951346540560851, 
    -2.03297777116994, -3.03791799221211, -1.6725667580037, -0.703729369742008, 
    -0.758435376013951, -1.67520097420276, -0.712699831144135, 
    -0.678858300665494, -4.55159741642415, -5.6702790821484, 
    -5.70385021022027, -4.5769504553531, -5.67919285240072, -6.71307383889938, 
    -0.53444363466226, -0.505230942174376, -0.501554990703022, 
    -0.509153131038228, -0.521897860827852, -0.52178557316689
    )), .Names = c("variable", "scenario", "Period", "value", 
"error", "ymin", "ymax"), class = "data.frame", row.names = c(NA, 
-24L))


ggplot(data=df, aes(x=Period, y=value)) +
     geom_bar(aes(fill=variable), position='dodge', stat="identity", width=0.75) +
     geom_errorbar(aes(ymin=ymin, ymax=ymax, width=.2)) +
     facet_wrap(~scenario) +
     theme_bw(base_size=16)

As you can see, the error bars are not properly aligned with the data bars. I've also tried a few solutions found on the web, such as:

geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.9)) 

but to no avail.

What is wrong with my ggplot command?


回答1:


You should add group = variable inside the aes() of ggplot() to make position_dodge() work.

ggplot(data=df, aes(x=Period, y=value, group = variable)) +
      geom_bar(aes(fill=variable), position='dodge', stat="identity", width=0.75) +
      geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.75),width = 0.2) +
      facet_wrap(~scenario) +
      theme_bw(base_size=16)

The same can be atchieved if you move the fill = variable to the aes() of ggplot() from geom_bar().

ggplot(data=df, aes(x=Period, y=value, fill = variable)) +
      geom_bar(position='dodge', stat="identity", width=0.75) +
      geom_errorbar(aes(ymin=ymin, ymax=ymax), position = position_dodge(0.75),width = 0.2) +
      facet_wrap(~scenario) +
      theme_bw(base_size=16)


来源:https://stackoverflow.com/questions/42772250/ggplot2-bars-non-aligned-to-error-bars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!