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

如何彻底移除sjPlot包plot_model绘图中残留的红色线条?

问题描述

使用sjPlot包的plot_model()函数绘制模型预测图时,已设置ci.lvl = NA试图移除红色置信区间线条,后续通过geom_line()添加了三条黑色线条,但仍有小部分红色线条残留(如图所示),求彻底移除的方法。

相关代码与数据

模型代码

Jitter_Mikro_temporal <- lmer(cyano ~ pH
                   + (1|month) +
                     (1|StationID),
                   data = count_all)
summary(Jitter_Mikro_temporal)

绘图代码

plot <- plot_model(Jitter_Mikro_temporal, type = "pred", terms = c("pH"), title = "Microscopy", size = 6, ci.lvl = NA)
plot.incl.full.SID
fig.incl.full.SID <- plot.incl.full.SID + theme_bw() +
  ylab(expression("Cyanobacteria [%]")) +
  xlab("pH") +
  geom_jitter(data = count_all, aes(x = pH, y = cyano, col = StationID), size = .1, alpha = 1)

fig.pH.2 <- fig.incl.full.SID + theme_bw() +
  ylab(expression("Cyanobacteria [%]")) +
  xlab("pH") +
  xlim(7,9) + 
  geom_jitter(data = count_all, aes(x = pH, y = cyano, col = StationID), size = .1, alpha = 1)

raw.1.micro <- fig.pH.2 +
  geom_line(data = pH_K, aes(x = pH, y = fit), color = "black", size = 1.5) +
  geom_line(data = pH_K, aes(x = pH, y = lower), color = "black", size = 1, linetype = 2) + 
  geom_line(data = pH_K, aes(x = pH, y = upper), color = "black", size = 1, linetype = 2)

raw.1.micro

模型摘要

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.1299 -0.2447 -0.1149  0.0564  5.5750 

Random effects:
 Groups    Name        Variance Std.Dev.
 month     (Intercept) 74.194   8.614   
 StationID (Intercept)  5.507   2.347   
 Residual              63.855   7.991   
Number of obs: 99, groups:  month, 12; StationID, 4

Fixed effects:
            Estimate Std. Error     df t value Pr(>|t|)  
(Intercept)    85.73      33.02  88.92   2.597   0.0110 *
pH            -10.30       4.09  87.91  -2.519   0.0136 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
   (Intr)
pH -0.996

部分数据

structure(list(StationID = c("Bork_W_1", "Bork_W_1", ...), ...), row.names = c(NA,50L), class = "data.frame")

绘图效果

残留红色线条的绘图

解决方法

方法1:手动移除置信区间相关图层

plot_model()返回的是ggplot对象,可直接查看并移除对应置信区间的图层:

# 查看所有图层,确认置信区间图层的标识
lapply(plot$layers, function(x) x$name)

# 移除所有包含置信区间(ci)的图层
plot_clean <- plot[!sapply(plot$layers, function(layer) {
  grepl("ci", layer$name) || ("ci" %in% names(layer$data))
})]

# 用清理后的plot继续后续绘图流程
fig.incl.full.SID <- plot_clean + theme_bw() +
  ylab(expression("Cyanobacteria [%]")) +
  xlab("pH") +
  geom_jitter(data = count_all, aes(x = pH, y = cyano, col = StationID), size = .1, alpha = 1)

方法2:强制设置置信区间颜色为透明

如果不想手动删除图层,可通过颜色映射强制将红色ci线条设为透明:

plot <- plot_model(Jitter_Mikro_temporal, type = "pred", terms = c("pH"), title = "Microscopy", size = 6, ci.lvl = NA) +
  scale_color_manual(values = c("black", "transparent", "transparent")) +
  scale_fill_manual(values = "transparent")

注:此方法需确认颜色顺序,若模型包含多个预测项可能需要调整参数值。

方法3:升级sjPlot包

部分旧版本的sjPlot可能存在ci.lvl=NA未完全生效的bug,升级到最新版本可解决:

install.packages("sjPlot")
library(sjPlot)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 10:09:49