如何移除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
相关产品推荐
相关产品推荐

