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

如何在同一图表绘制GLM/LM中两种生物量的回归直线?

问题描述

我正在分析原生生物量(Native_biomass)与非原生生物量(Non_native_biomass)对鸟类繁殖指标的影响,已构建包含控制变量的线性模型,输出如下:

Estimate Std. Error t value Pr(>|t|)   
(Intercept)         45.38841    9.55586   4.750  0.00208 **
Native_biomass      -0.50256    0.18359  -2.737  0.02903 * 
Non_native_biomass  -0.16094    0.09672  -1.664  0.14007   
SpeciesGreat_tit     0.29105    4.21291   0.069  0.94685   
No_tree_species     -0.77424    0.58555  -1.322  0.22765   
Distance_to_light   -0.28324    0.14644  -1.934  0.09435 . 
Anthropogenic_cover -0.02871    0.06234  -0.461  0.65909   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

目前我用effect_plot(jtools包)生成了两个独立的回归图:

effect_plot(lm5, pred= Native_biomass, 
            interval = TRUE, plot.points = TRUE,
            x.label="Native Biomass %", y.label="Lay day of first egg",
            main.title = "Effect of increasing native biomass on the day 
                    of first egg being layed",
            colors="seagreen", point.alpha=0.6, rug=TRUE)

effect_plot(lm5, pred= Non_native_biomass, 
            interval = TRUE, plot.points = TRUE,
            x.label="Non_native_biomass %", y.label="Lay day of first egg",
            main.title = "Effect of increasing non-native biomass on 
the day of first egg being layed",
            colors="seagreen", point.alpha=0.6, rug=TRUE)

需求:

  • 将两条回归线绘制在同一图表中,X轴标注为“生物量”,用不同颜色区分原生与非原生生物量的回归线
  • 支持对2个LM模型和2个GLM模型执行该操作,共生成4个图表

解决方案

核心思路

effect_plot本身不支持直接叠加多个预测变量的效应,因此我们先提取两个生物量变量的预测效应数据,再用ggplot2手动绘制合并图——这种方法同时兼容LM和GLM模型,灵活性更高。

步骤1:安装/加载所需包

# 未安装则先安装
install.packages(c("jtools", "ggplot2", "dplyr", "tidyr"))

# 加载包
library(jtools)
library(ggplot2)
library(dplyr)
library(tidyr)

步骤2:编写通用绘图函数

写一个可复用的函数,输入模型、标签、颜色等参数,自动生成合并图:

plot_combined_biomass <- function(model, y_label = "首枚产卵日", 
                                  title = "生物量对繁殖性状的影响",
                                  native_color = "#2E8B57", non_native_color = "#FF6347") {
  
  # 提取原生生物量的效应数据
  native_eff <- effect_plot(model, pred = Native_biomass, interval = TRUE, 
                            plot.points = FALSE, return_data = TRUE) %>%
    mutate(Biomass_Type = "原生生物量")
  
  # 提取非原生生物量的效应数据
  non_native_eff <- effect_plot(model, pred = Non_native_biomass, interval = TRUE, 
                                plot.points = FALSE, return_data = TRUE) %>%
    mutate(Biomass_Type = "非原生生物量")
  
  # 合并效应数据
  combined_data <- bind_rows(native_eff, non_native_eff)
  
  # 提取原始散点数据(替换成你的实际数据集名称和响应变量名)
  scatter_data <- model$model %>%
    select(响应变量名, Native_biomass, Non_native_biomass) %>%
    pivot_longer(cols = c(Native_biomass, Non_native_biomass),
                 names_to = "Biomass_Type", values_to = "Biomass") %>%
    mutate(Biomass_Type = ifelse(Biomass_Type == "Native_biomass", "原生生物量", "非原生生物量"))
  
  # 绘制合并图
  ggplot() +
    # 原始散点
    geom_point(data = scatter_data, aes(x = Biomass, y = 响应变量名, color = Biomass_Type), 
               alpha = 0.6) +
    # 回归线和置信区间
    geom_line(data = combined_data, aes(x = x, y = predicted, color = Biomass_Type), linewidth = 1) +
    geom_ribbon(data = combined_data, aes(x = x, ymin = conf.low, ymax = conf.high, fill = Biomass_Type), 
                alpha = 0.2) +
    # 轴标签与标题
    labs(x = "生物量 (%)", y = y_label, title = title, color = "生物量类型", fill = "生物量类型") +
    # 主题与颜色设置
    theme_minimal() +
    scale_color_manual(values = c("原生生物量" = native_color, "非原生生物量" = non_native_color)) +
    scale_fill_manual(values = c("原生生物量" = native_color, "非原生生物量" = non_native_color))
}

步骤3:批量生成4个模型的图表

假设你的4个模型分别为lm_model1、lm_model2、glm_model1、glm_model2,直接调用函数即可:

# LM模型1
plot_combined_biomass(lm_model1, title = "LM模型1:生物量对首枚产卵日的影响")

# LM模型2
plot_combined_biomass(lm_model2, title = "LM模型2:生物量对首枚产卵日的影响")

# GLM模型1(根据实际响应变量调整y_label,比如繁殖成功率)
plot_combined_biomass(glm_model1, y_label = "繁殖成功率", title = "GLM模型1:生物量对繁殖成功率的影响")

# GLM模型2
plot_combined_biomass(glm_model2, y_label = "雏鸟数量", title = "GLM模型2:生物量对雏鸟数量的影响")

关键说明

  1. 数据匹配:需将函数中响应变量名和数据集名称替换为你实际的变量/数据集名称
  2. GLM适配:如果是logistic等类型的GLM,effect_plot会自动处理预测值转换(如概率),无需额外修改函数,仅需调整y_label
  3. 样式自定义:可通过修改native_color、non_native_color参数更换颜色,或添加theme()调整图表细节

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 17:54:26