ggplot2-条与误差条不对齐 [英] ggplot2 - Bars non-aligned to error bars

查看:111
本文介绍了ggplot2-条与误差条不对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot2在R中的条形图上绘制误差线。条形图的位置尚可,但是误差条未对齐,重叠。请看下面的例子:

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

但无济于事。

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

What is wrong with my ggplot command?

推荐答案

您应该添加 group =变量 ggplot() aes()内以生成 position_dodge()工作。

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)

如果将 fill =变量移至<$ c来自 geom_bar() ggplot()中的$ c> aes()。

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)

这篇关于ggplot2-条与误差条不对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆