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

如何移除ggeffects绘制GEE模型预测概率图的离散点连接线?

问题:移除ggeffects绘制GEE模型预测概率图中的连接线

问题背景

  • 使用ggeffects绘制geepack构建的多变量GEE模型预测概率,已设置options(ggeffects::ggeffects_margin = "marginalmeans")
  • 模型包含编码为1、2的二元预测变量,基于ggeffects 1.5.0编写的旧代码运行出错,重装该版本仍无法解决
  • 尝试通过connect_lines = FALSE参数移除连接离散点的线条,但图表无变化
  • 当前使用的包版本:
    [1] stringr_1.5.1   forcats_1.0.0   patchwork_1.2.0 ggeffects_1.7.2
    [5] geepack_1.3.12  gtsummary_2.0.3 ggplot2_3.4.4   dplyr_1.1.4
    

解决方案

方法1:明确指定变量为离散型

在predict_response中添加type = "discrete"参数,让函数识别目标变量为离散类型,绘图时默认不会生成连接线:

ggeffects::plot(
  predict_response(GEE模型, terms = "x", type = "discrete"),
  ci_style = "errorbar"
) +
  ggplot2::scale_x_continuous(
    labels = c("\n No", "\n Yes"),
    breaks = c(1, 2),
    expand = c(0,0)
  )

方法2:手动移除线条图层

如果方法1无效,直接通过ggplot2的图层管理删除线条元素:

# 生成基础图
p <- ggeffects::plot(
  predict_response(GEE模型, terms = "x"),
  ci_style = "errorbar"
) +
  ggplot2::scale_x_continuous(
    labels = c("\n No", "\n Yes"),
    breaks = c(1, 2),
    expand = c(0,0)
  )

# 过滤掉线条图层
p$layers <- p$layers[-which(sapply(p$layers, function(x) inherits(x$geom, "GeomLine")))]
print(p)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 19:02:46