6

我的条形图有问题 - 误差条只出现在分组变量列的角落,而不是集中显示在它们上。我正在使用的代码是这样的:

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

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

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

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

我非常感谢这方面的帮助。

谢谢!

4

2 回答 2

15

好问题。一些评论:

  1. 一般来说,在原始调用中设置所有美学是一个很好的做法,并且只有在个别调用ggplot()中需要时才用不同的美学覆盖它们。geom_xyz()在您的代码中,您设置了两次填充美学,一次是在ggplot,一次是在geom_bar. 您还可以在geom_errorbar(). 我不认为这些事情是最终问题,但它们确实使调试代码变得更加困难。

  2. 主要问题是widthin 的参数必须与insidegeom_bar的参数匹配。所以如果你有position_dodge()geom_errorbar

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

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

    # ...
    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')

最终图表

于 2016-02-16T05:28:39.323 回答
0

我想补充一下,因为我遇到了同样的问题:

在 main中而不是在 geom_barplot中指定fill参数非常重要。aes()

于 2019-02-28T16:30:42.487 回答