繁体   English   中英

将错误栏放在ggplot()中列的中心时出现问题

[英]Problem placing error bars at the center of the columns in ggplot()

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

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

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

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

谢谢!

好问题。 几条评论:

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

  2. 主要的问题是, width在参数geom_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')

最终图表

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

在主aes()中指定“fill”参数非常重要,而不是在geom_barplot中指定。

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM