You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在ggplot中纳入proportion_oac作为协变量绘图时遇错求助

解决ggplot中geom_smooth引用协变量报错的问题

现有如下数据框:

year_group total_AF total_strokes proportion_oac    Sex proportion_per_1000_AF
1   2000-2002      373            10    0.402144772 Female              26.809651
2   2003-2005      557             7    0.359066427 Female              12.567325
3   2006-2008      911             9    0.290889133 Female               9.879254
4   2009-2011     1481            17    0.064145847 Female              11.478731
5   2012-2014     2153            29    0.035764050 Female              13.469577
6   2015-2017     2760            30    0.017028986 Female              10.869565
7   2018-2020     3626            42    0.013513514 Female              11.583012
8   2021-2023     1021             8    0.007835455 Female               7.835455
9   2000-2002      889            18    0.428571429   Male              20.247469
10  2003-2005     1397            20    0.382247674   Male              14.316392
11  2006-2008     1969            33    0.347892331   Male              16.759777
12  2009-2011     2686            31    0.093447506   Male              11.541325
13  2012-2014     3802            42    0.041031036   Male              11.046817
14  2015-2017     4476            46    0.025245755   Male              10.277033
15  2018-2020     5300            64    0.014528302   Male              12.075472
16  2021-2023     1342            12    0.013412817   Male               8.941878

已成功绘制分组柱状图:

ggplot(df_strokeAF, aes(x = year_group, y = proportion_per_1000_AF, fill = Sex)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(x = "Year Group", y = "Proportion per 1000 AF", fill = "Sex") +
  theme_minimal()

尝试添加调整proportion_oac作为协变量的平滑线时,使用以下代码报错:

ggplot(df_strokeAF, aes(x = year_group, y = proportion_per_1000_AF, fill = Sex)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  labs(x = "Year Group", y = "Proportion per 1000 AF", fill = "Sex") +
  geom_smooth(aes(group = Sex, color = Sex), method = "lm", formula = y ~ x * proportion_oac, se = FALSE) +
  theme_minimal()

报错信息:

Warning message:
Computation failed in stat_smooth()
Caused by error:
! object 'proportion_oac' not found


问题原因

  1. geom_smooth的formula参数中,x仅指代映射到x轴的变量(此处为分类变量year_group),无法直接在公式中引用数据框的其他列(如proportion_oac)。
  2. year_group是分类因子,直接作为线性模型自变量不符合建模逻辑,且geom_smooth默认处理方式不支持包含额外协变量的模型拟合。

解决方案

要实现调整协变量后的趋势线,需先拟合包含协变量的线性模型,生成调整后的预测值,再将预测值添加到图表中:

  1. 拟合模型并生成预测值
# 拟合包含year_group、Sex交互项及proportion_oac协变量的线性模型
model <- lm(proportion_per_1000_AF ~ year_group * Sex + proportion_oac, data = df_strokeAF)

# 生成调整后的预测值,合并到原数据框
df_strokeAF$predicted <- predict(model, newdata = df_strokeAF)
  1. 绘制包含趋势线的最终图表
ggplot(df_strokeAF, aes(x = year_group, y = proportion_per_1000_AF, fill = Sex)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  # 添加调整后的趋势线,与柱状图对齐
  geom_line(aes(y = predicted, color = Sex, group = Sex), position = position_dodge(width = 0.9)) +
  labs(x = "Year Group", y = "Proportion per 1000 AF", fill = "Sex", color = "Sex") +
  theme_minimal()

说明

  • 模型中加入year_group与Sex的交互项,同时纳入协变量proportion_oac,确保预测值是调整该协变量后的结果。
  • position_dodge(width = 0.9)用于让趋势线和对应柱状图对齐,宽度与geom_bar默认的dodge宽度匹配。

内容的提问来源于stack exchange,提问作者MaxStudent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 22:27:48