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

在ggplot2中绘制含交互项的共截距分组斜率回归线

解决ggplot2中绘制共享截距、分组斜率回归线的问题

你的需求是展示分类变量treatment各水平对应time的不同斜率,但共享同一截距的回归结果,对应模型lm(measurement ~ time:treatment, data = example_data)。直接在geom_smooth中使用y ~ x:treatment会报错,而用y~x会生成各分组独立的截距,以下是两种可行的解决方法:

方法一:预拟合模型+生成预测值绘制

这种方式能严格匹配你想要的全局模型,先拟合模型后生成预测数据,再用线条绘制回归线:

library(tidyverse)

set.seed(42)
example_data = 
data.frame(treatment   = as.factor(c(rep("t1", 50), rep("t2", 50))),
           time        = rep(1:50, 2), 
           error       = rnorm(100, 0, 1))                      %>% 
mutate(    slope       = ifelse(treatment == "t1", -0.2, -0.3)) %>% 
mutate(    measurement = 2.5 + time * slope + error)          

# 拟合目标模型
model = lm(measurement ~ time:treatment, data = example_data)

# 生成包含模型预测值的数据集
pred_data = example_data %>%
  mutate(pred = predict(model, newdata = .))

# 绘制散点+回归线
ggplot(data = example_data, aes(x = time, y = measurement)) + 
  geom_point(alpha = 0.6) +
  # 按treatment分组绘制预测回归线
  geom_line(data = pred_data, aes(y = pred, color = treatment), linewidth = 1) +
  facet_wrap(~treatment) +
  theme_bw()

方法二:调整geom_smooth参数实现全局拟合

无需手动生成预测值,通过指定geom_smooth使用全局数据拟合,同时将treatment纳入映射,避免分面板单独拟合:

ggplot(data = example_data, aes(x = time, y = measurement, color = treatment)) + 
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", 
              formula = y ~ x:treatment,
              data = example_data, # 强制使用全局数据集拟合,而非面板子集
              se = FALSE, # 可选:关闭置信区间(全局模型的置信区间在分面板展示意义有限)
              linewidth = 1) +
  facet_wrap(~treatment) +
  theme_bw()

报错原因说明

之前的代码报错是因为geom_smooth默认对每个facet面板的子集数据单独处理,面板内仅存在一种treatment水平,导致x:treatment的交互项无法识别全局的treatment变量;而用y~x会让每个面板独立拟合带截距的模型,自然无法实现共享截距的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 16:24:53