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

使用plot_model绘制混合效应模型回归图时按随机效应对数据点着色

问题解答

你提出的需求完全可以实现。plot_model() 函数返回的是标准 ggplot2 绘图对象,我们可以通过关闭默认统一着色的原始数据点、叠加自定义分组点图层的方式,实现按随机效应Yr给数据点分配不同颜色,同时仅保留全局固定效应的单一回归线,不需要为每个随机效应水平单独生成回归线。

实现代码

你只需要修改原有绘图逻辑,先关闭默认数据点显示,再叠加自定义的点图层即可:

# 加载依赖包(如已加载可跳过)
library(lme4)
library(sjPlot)
library(ggplot2)

# 你原有数据构造、模型构建代码保持不变
dat <- data.frame(c(1, 1,  1,  1,  1,  1,  1,  4,  1,  2,  1,  2,  3,  1,  
                             5,  1,  2,  2,  1,  1,  2,  2,  4,  3,  5,  5,  4,  4,
                             8,  4, 13,  8,  6,  5,  6,  5, 4,  8,  6,  2,  3,  9, 7,
                             4,  8,  8, 10, 10,  3,  3), 
                           c(427.0, 110.0, 99.0, 450.0, 182.0,  34.0,  43.0, 105.0, 
                             22.0, 108.0,  85.0, 142.0, 81.0, 109.0,  89.0, 183.0, 
                             106.0, 19.3, 13.5,  36.0, 55.0, 36.0, 128.0,  38.2, 103.0, 
                             105.0,  91.0,  94.0, 106.0,  96.0, 108.0, 103.0, 122.0, 
                             103.0, 112.0, 190.0, 112.0, 77.0,  60.0, 148.0, 161.0, 
                             44.0, 188.0,  44.0,  88.0,  90.0, 124.0, 120.0, 212.0, 309.0),
                           c(32.3, 27.0, 26.7, 33.0, 28.4, 26.7, 28.0, 30.0, 26.5, 
                             28.4, 29.0, 30.4, 25.8, 26.4, 24.2, 28.6, 29.4, 27.7, 
                             25.5, 27.3, 27.4, 29.2, 21.5, 25.4, 27.8, 27.8, 30.2, 
                             30.2, 30.9, 30.0, 30.3, 31.5, 31.3, 31.1, 32.5, 32.0, 
                             32.2, 32.9, 32.9, 31.3, 32.3, 29.4, 31.5, 29.8, 26.7,
                             26.7, 29.9, 29.9, 32.1, 32.1),
                           c(7.73, 1.83, 2.97, 7.66, 1.80, 1.17, 0.66, 6.04, 1.72, 
                             1.25, 2.27, 1.65, 0.16, 0.16, 0.26, 0.11, 0.17, 0.15, 
                             0.20, 0.21, 0.15, 0.10, 1.83, 1.84, 7.67, 7.67, 5.81, 
                             5.81, 3.95, 3.45, 1.15, 3.07, 3.61, 3.35, 0.86, 2.20, 
                             4.31, 1.64, 1.14, 3.55, 4.81, 1.36, 1.94, 3.36, 3.30,
                             3.30, 2.66, 2.66, 6.55, 6.55),
                           c(94, 151, 111,  94,  87, 128, 126,  39, 130, 107,  93, 
                             110,  76, 309,  38,  19,  13, 113, 110, 108, 103,  30,
                             58, 151,  55,  55,  42, 42,  29,  37,  20,  20,  61, 
                             21,   3,   8,  28,  33,  32,  12,  38,  -7,  -5,  74,  
                             22,  22,  10,  10,  22, 22),
                           c("2005", "2005", "2005", "2005", "2005", "2005", "2005",
                             "2018", "2005", "2005", "2005", "2005", "2010", "2010",
                             "2010", "2010", "2010", "2010", "2010", "2010", "2010",
                             "2010", "2012", "2012", "2017", "2017", "2017", "2017",
                             "2017", "2017", "2017", "2017", "2017", "2017", "2018",
                             "2018", "2018", "2018", "2018", "2018", "2018", "2018",
                             "2018", "2018", "2018", "2018", "2018", "2018", "2018", 
                             "2018"))
colnames(dat) <- c("SpR", "Dep", "Sal", "Chla", "Ice", "Yr")

MEM <- lmer(formula = SpR ~ (1|Yr) +  
                Dep + Sal + Chla + Ice + 
                Dep*Ice, 
              data = dat)

# 绘制基础回归图,关闭默认数据点
MEMpld <- plot_model(MEM, 
                   type = "pred", 
                   pred.type = "fe", 
                   terms = c("Dep"), 
                   show.data = FALSE,
                   col = "black",
                   title = "",
                   jitter = 0.0002)

# 叠加按Yr分组着色的原始数据点
MEMpld + 
  geom_point(data = dat, aes(x = Dep, y = SpR, color = Yr), 
             size = 3.5, alpha = 1,
             position = position_jitter(width = 0.0002)) +
  labs(color = "年份") # 可自定义图例名称

可选调整

  • 如果需要同时用形状区分不同年份,可在geom_point的aes()中添加shape = Yr映射
  • 可以通过scale_color_manual()、scale_color_brewer()等函数自定义分组配色
  • 所有ggplot2支持的样式调整规则都可以直接应用在该绘图对象上

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 15:42:00