ggplot2 - 与错误条不对齐的条形图

时间:2017-03-13 19:51:54

标签: r ggplot2 alignment bar-chart errorbar

我正在尝试使用ggplot2在R中的条形图上绘制误差条。条的位置没问题,但误差条未对齐,重叠。请看下面的示例:

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)

enter image description here

如您所见,错误栏未与数据栏正确对齐。我也尝试过在网上找到的一些解决方案,例如:

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

但无济于事。

我的ggplot命令出了什么问题?

1 个答案:

答案 0 :(得分:3)

您应该在group = variable aes()内添加ggplot(),以使position_dodge()正常工作。

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)

如果将fill = variableaes()移至ggplot() 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)