在ggplot()中将错误栏放置在列中心时出现问题 [英] Problem placing error bars at the center of the columns in ggplot()

查看:129
本文介绍了在ggplot()中将错误栏放置在列中心时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的条形图有问题-错误条仅出现在分组变量列的角上,而不是以集中方式出现在它们上.我正在使用的代码是这样的:

I am having a problem with my bar chart- the error bars just appear on the corner of the columns of the grouping variable rather than on them in a centralised way. The code I am using is this:

a <- data.frame (Cond = c("In", "In", "Out", "Out"),
                Temp = c("Hot", "Cool", "Hot", "Cool"),
                Score = c(.03, -.15, 0.84, 0.25),
                SE = c(.02, .08, .14, .12))
a.bar <- ggplot (data = a, aes(x = Cond, y = Score, fill = Temp)) +
            theme_bw() + theme(panel.grid = element_blank ()) +
            coord_cartesian (ylim = c(-0.5, 1)) + 
            geom_bar (aes(fill = Temp), stat = "identity", position = "dodge", width = .5) +
            geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.9), width = .08) +
            labs(y = "Scores" , x = "Cond") +
            scale_y_continuous (breaks = pretty_breaks(n=8)) +
            theme(legend.title = element_blank()) +
            theme(legend.position = "right")

我尝试过的替代代码,我也无法使用,包括在geom_bar()中添加"show.legend = FALSE";添加"facet_wrap(〜Cond)"图.并在ggplot(aes())中引入"fill = Temp". 最接近的解决方案是当我将position_dodge()参数更改为:

The alternative codes I have tried, which I couldn't get to work either, included adding "show.legend = FALSE" to geom_bar(); adding "facet_wrap(~Cond)" plot.a; and introducing "fill = Temp" within ggplot(aes()). The closest solution was when I changed the position_dodge() argument into:

geom_bar (aes(fill = Temp), stat = "identity", position = position_dodge(width = .5)) +
geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.5), width = .08) +

(其余代码保持不变).这将误差线移向列的中心,但同时也将列移向彼此,最终使它们重叠(请参见附图).

(the rest of the code remains the same). This moved the error bars towards the center of the columns, yet also moved the columns towards each other, eventually making them overlap (see attached figure).

非常感谢您提供帮助.

谢谢!

推荐答案

很好的问题.一些评论:

Nice question. A couple of comments:

  1. 通常,在原始的ggplot()调用中设置所有美学,并仅在单独的geom_xyz()调用中需要时才使用不同的美学来覆盖它们,这是一种好习惯.在您的代码中,您两次设置填充美学,一次在ggplot中,一次在geom_bar中.您还可以在geom_errorbar()中设置组的美观度.我认为这些都不是最终的问题,但是它们确实使调试代码变得更加困难.

  1. In general, it's a good practice to set all your aesthetics in the original ggplot() call, and to override them with different aesthetics only if needed in the individual geom_xyz() calls. In your code, you were setting the fill aesthetic twice, once in ggplot and once in geom_bar. You also set the group aesthetic in geom_errorbar(). I don't think any of those things were the ultimate problem, but they did make it harder to debug your code.

主要问题是geom_bar中的width自变量必须与geom_errorbar中的position_dodge()自变量匹配.所以如果你有

The main problem was that the width argument in geom_bar has to match the position_dodge() argument inside geom_errorbar. So if you have

# ...
geom_bar(stat = "identity", position = "dodge", width = 0.5)
# ...

然后您必须确保geom_errorbar()看起来像

Then you've got to make sure your geom_errorbar() looks like

# ...
geom_errorbar(width = .08, position = position_dodge(0.5)) 
# ...

将它们放在一起:

require(ggplot2)
require(scales)

# define data
a <- data.frame (Cond = c("In", "In", "Out", "Out"),
                Temp = c("Hot", "Cool", "Hot", "Cool"),
                Score = c(.03, -.15, 0.84, 0.25),
                SE = c(.02, .08, .14, .12))

# return plot with everything except error bars
a.bar <- ggplot (data = a, aes(x = Cond, 
                               y = Score, 
                               fill = Temp,
                               ymin = Score - SE,
                               ymax = Score + SE)) +
            theme_bw() + 
            theme(panel.grid = element_blank ()) +
            coord_cartesian(ylim = c(-0.5, 1)) + 
            # manually setting the width means we will have to tell geom_errorbar() about the new width
            geom_bar(stat = "identity", position = "dodge", width = 0.5) + 
            labs(y = "Scores", x = "Cond") +
            scale_y_continuous(breaks = pretty_breaks(n = 8)) +
            theme(legend.title = element_blank()) +
            theme(legend.position = "right")

# show plot w/ errorbars, note that argument to position_dodge is same as width supplied above
a.bar + geom_errorbar(width = .08, position = position_dodge(0.5)) 

# save results
ggsave('SO_35424162.png')

这篇关于在ggplot()中将错误栏放置在列中心时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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