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

如何为plot_model添加CI误差须与垂直线?

解决plot_model无法显示CI误差须和添加垂直线的问题

一、修复CI误差须不显示的问题

当可视化两个二分类变量的预测结果时,plot_model默认的置信区间样式(ci.style="ribbon")对分类x轴不友好,需要显式指定误差须样式:

  • 添加ci.style="errorbar"参数,强制绘制误差须
  • 保留ci=TRUE和ci.lvl=0.95参数确保置信区间计算逻辑生效

修改后的核心代码片段:

plot_model(my_model, type = "pred", terms = c("Language", "Switching"),
           axis.title = "Response Times (log)", legend.title = "Condition",
           colors = "Set2", dot.size = 3, line.size = 1.5, 
           ci = TRUE, ci.lvl = 0.95, ci.style = "errorbar")

二、添加垂直线的方法

plot_model返回的是ggplot对象,可直接叠加ggplot图层实现垂直线:

  • 使用geom_vline()添加垂直参考线,通过xintercept指定线的位置(若x轴是字符型分类变量,需用as.numeric(factor("类别名称"))转换为对应位置数值;若是数值编码则直接填对应数值)
  • 可通过linetype(线条样式)、color(颜色)、linewidth(线宽)自定义线条外观

示例代码(假设要在Language的第二个类别位置添加红色虚线):

plot_model(my_model, type = "pred", terms = c("Language", "Switching"),
           axis.title = "Response Times (log)", legend.title = "Condition",
           colors = "Set2", dot.size = 3, line.size = 1.5, 
           ci = TRUE, ci.lvl = 0.95, ci.style = "errorbar") +
  geom_vline(xintercept = 2, linetype = "dashed", color = "red", linewidth = 1)

额外注意事项

  • 若模型为广义线性模型(GLM),需确认模型类型支持预测置信区间的计算
  • 若x轴分类顺序不符合预期,可先用factor()函数调整变量水平顺序后再绘图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:45:09