在ggplot2图表中更改误差条的颜色。

3

我在ggplot中的条形图中遇到了误差棒问题。 我有一个分类(条件)和连续(调节变量)变量之间的交互作用。我想显示误差棒,但它们与条形图颜色相同,这使它们难以解释。 我尝试为误差棒添加color =“black”等颜色,但它不会改变任何东西。 以下是代码:


moderator = runif(n = 100, min = 1, max = 7)
condition <- rep(letters[1:2], length.out = 100)
y = runif(n = 100, min = 1, max = 100)

df <- data.frame(moderator, condition, y)


lm21 <- lm(y~ condition* moderator, data = df)
summary(lm21) 

library(ggeffects)
library(ggplot2)
library(magrittr)


    pd <- position_dodge() 
ggeffect(lm21, terms = c("condition", "moderator")) %>%  
  plot(show.title = FALSE) +
  stat_summary(fun.y = mean, geom = "bar", position = pd, width = 0.25) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
               position = pd, size = 8.5, alpha=13.2) +
  scale_y_continuous("Voting", limits = c(0, 100)) + 
  scale_color_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  scale_x_continuous(name = "Condition",
                     breaks = 0:1,
                     labels = c("Low","High")) 

图形如下所示:

enter image description here

如何改变误差线的颜色,以便它们完全可见?

提前致谢!

3个回答

3
我试图将ggeffect值转换为数据框,最终结果如下,希望这是你所想要的。 抱歉,宽度控制是手动完成的,我尝试调整以使其居中。也许有人比我更擅长,知道如何操作。
  ggplot(as.data.frame(ggeffect(lm21, terms = c("condition", "moderator"))), aes(x = factor(x))) +
    geom_col(aes(y = predicted, fill = factor(group)), position = position_dodge2(width = .5, preserve = "single", padding = 0)) +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high, group = factor(group)), position = position_dodge(width = .9), width = .15) +
    geom_point(aes(y = predicted, group = factor(group)), position = position_dodge2(width = .9)) +
    scale_fill_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
    scale_y_continuous("Voting", limits = c(0, 100)) +
    scale_x_discrete(name = "Condition", labels = c("Low","High")) +
    theme_light()

enter image description here


太棒了,完美地运行!非常感谢你! - Anna Potoczek

2

好的,虽然这不是最简单的方法,但这是我所做的:

p = ggeffect(lm21, terms = c("condition", "moderator")) %>%  
  plot(show.title = FALSE) +
  stat_summary(fun.y = mean, geom = "bar", position = pd, width = 0.25) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
               position = pd, size = 8.5, alpha=13.2) +
  scale_y_continuous("Voting", limits = c(0, 100)) + 
  scale_color_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  scale_x_continuous(name = "Condition",
                     breaks = 0:1,
                     labels = c("Low","High"))+
  scale_colour_manual(values = rep('black',3))+
  theme(legend.position = 'none') 

输出结果如下:

enter image description here

唯一的问题是图例已经丢失,因为scale_colour_manual改变了它。但是您可以使用此帖子提取图例How to plot just the legends in ggplot2? 并将其与您的图形合并。
我希望这就是您想要的。

谢谢您的回答,我一定会尝试这个方法。不过,是否也可以添加误差条的下部分,例如这里的图片? https://pythonforundergradengineers.com/images/bar_plot_with_error_bars.png - Anna Potoczek

1
这是另一种基于grobs操作的解决方案。
p <- ggeffect(lm21, terms = c("condition", "moderator")) %>%  
  plot(show.title = FALSE) +
  stat_summary(fun.y = mean, geom = "bar", position = pd, width = 0.25) +
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
               position = pd, size = 8.5, alpha=13.2) +
  scale_y_continuous("Voting", limits = c(0, 100)) + 
  scale_color_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  scale_x_continuous(name = "Condition",
                     breaks = 0:1,
                     labels = c("Low","High")) 

# Change the order of ggplot layers (error bars are printed after mean bars)
p$layers <- p$layers[c(3,1,2,4)]

# Set colors of polyline grob (error bars)
q <- ggplotGrob(p)
q$grobs[[6]]$children[[5]]$gp$col <- rep("black",6)
grid::grid.draw(q)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接