ggplot2 中的误差线和线条不匹配

Posted

技术标签:

【中文标题】ggplot2 中的误差线和线条不匹配【英文标题】:Error bars and lines do not match in ggplot2 【发布时间】:2021-10-14 23:12:01 【问题描述】:

我在 ggplot2 中遇到错误栏问题。

我想绘制连续 [iv] 和标称 [条件] 变量之间的交互。 我用这样的代码

iv = 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(iv, condition, y)

lm20 <- lm(y ~ condition * iv, data = df)
summary(lm20) 

df$condition <- as.factor(df$condition)

ggeffect(lm20, terms = c("condition", "iv")) %>%  
  plot(show.title = FALSE) + 
  stat_summary(fun.y = mean,
               geom = "point") +
  stat_summary(fun.y = mean,
               geom = "line") +
  stat_summary(fun.data = mean_cl_boot,
               geom = "errorbar") +
  scale_y_continuous("Voting intentions", limits = c(0, 100)) + 
  scale_colour_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
  scale_x_discrete("Condition", labels = c("Low","High")

这就是我得到的:

我遇到两个问题

    误差线和线不匹配 我无法更改 X 轴上的标签

【问题讨论】:

【参考方案1】:

下面是一个只使用 ggplot2 的解决方案,所以没有那么优雅……看看它是否能解决问题!

# Create data frame with the effects    
df <- as.data.frame(ggeffect(lm20, terms = c("condition", "iv")))
# Add coordinates manually
df$x2 <- rep(1:2, 3) + rep(c(-.1,0,.1), each = 2)

# Plot
ggplot(df, aes(x2, predicted, col = group)) + 
  geom_errorbar(aes(x = x2, ymin = predicted-std.error, ymax = predicted+std.error), width = .1) +
  geom_segment(df %>% pivot_wider(id_cols = group, names_from = x,  values_from = c(predicted:conf.high, x2)),
               mapping = aes(x = x2_a, xend = x2_b, y = predicted_a, yend = predicted_b)) +
  geom_point(size = 2) +
  scale_color_discrete("Control",c("#9b2756", "#003054","#66b0a5"), labels=c("Low", "Medium", "High")) +
  scale_y_continuous("Voting intentions", limits = c(0, 100)) + 
  scale_x_continuous("Condition", breaks = 1:2, labels = c("Low","High"))

结果如下:

【讨论】:

太好了,非常感谢 - 很高兴知道单独使用 ggplot2 是可能的!【参考方案2】:

这里发生了几件事。主要是:

您需要设置一个 position_dodge 来替换您添加的点,因为 ggeffects 已经替换了错误栏 ggeffects 实际上是使用连续的 x 轴刻度。请参阅下面我对scale_x_continuous() 的使用... mean_cl_boot 好像什么都没做,还没弄明白为什么
library(ggeffects)
library(ggplot2)
library(magrittr)

pd <- position_dodge(width=0.25)  ## width set by trial & error to match ggeffects plot
ggeffect(lm20, terms = c("condition", "iv")) %>%  
    plot(show.title = FALSE) +
    ## set large size/alpha to distinguish mean points from the 
    ##   (identical) points that ggeffects is already using for the estimated values
    stat_summary(fun = mean, geom = "point", position = pd, size = 5,
                 alpha=0.5) +
    stat_summary(fun = mean, geom = "line", position = pd) +
    stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
                 position = pd, size = 5, alpha=0.5) +
    scale_y_continuous("Voting intentions", limits = c(0, 100)) + 
    scale_colour_discrete(name = "Control", labels = c("Low", "Medium", "High")) +
    scale_x_continuous(name = "Condition",
                       breaks = 1:2,
                       labels = c("Low","High"))

【讨论】:

太棒了,非常感谢!太有帮助了!

以上是关于ggplot2 中的误差线和线条不匹配的主要内容,如果未能解决你的问题,请参考以下文章

R语言R原生以及ggplot2设置线条类型宽度(粗细)颜色的函数ggplot2手动自定义设置线条类型粗细颜色函数(line typesthicknesscolour)

即使我使用数字,ggplot2中的离散值/连续比例误差

如何在ggplot2中分别缩放线和点的大小

缺少线条ggplot2图

为线条 ggplot2 分配颜色

R语言ggplot2可视化:默认情况下ggplot2在x轴和y轴的刻度线和轴之间保留了一些空间设置ggplot2可视化去除可视化结果与坐标轴之间的空间可视化结果与坐标轴紧紧贴合,没有空白区域