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

如何修改sjPlot包plot_model函数绘制回归图的分面标签

你可以通过修改ggplot分面的标签器实现自定义分面名称,两种常用方案如下:

方案1:直接修改返回的ggplot对象分面设置(最灵活)

plot_model返回的是标准ggplot对象,你只需要在原有绘图代码基础上追加facet_wrap配置,覆盖原有分面的标签逻辑即可:

# 第一步:先查看plot_model实际用于分面的C的取值,确保标签对应
unique(p1$data$C)

# 第二步:自定义分面标签,命名向量的名称要和上面查到的取值完全一致
custom_c_labs <- c(
  "0.11" = "This is C=0.11",
  "0.39" = "This is C=0.39",
  "666" = "This is C=666"
)

# 第三步:在原有绘图代码中增加分面标签配置
p1_f <- p1 + 
  geom_line(size = 1) + 
  xlim(0, 10.00) + 
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "top"
  ) +
  # 覆盖原有分面的标签设置
  facet_wrap(~C, labeller = labeller(C = custom_c_labs))

方案2:自定义分面的C取值+标签

如果你连分面使用的C的取值都想自定义,不需要用默认的均值±标准差,可以直接在plot_model的terms参数里指定取值,再搭配标签修改:

# 比如指定C取0.1、0.3、0.5三个值做分面
p1 <- plot_model(
  model1, 
  type = "pred",  
  show.p = TRUE,
  # 方括号内直接写你要展示的C的取值
  terms = c("A", "B", "C [0.1, 0.3, 0.5]"), 
  axis.title = c("", "" ), 
  title = ""
)

# 再自定义对应标签即可
custom_c_labs <- c(
  "0.1" = "This is C=0.1",
  "0.3" = "This is C=0.3",
  "0.5" = "This is C=0.5"
)

p1_f <- p1 + 
  geom_line(size = 1) + 
  xlim(0, 10.00) + 
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "top"
  ) +
  facet_wrap(~C, labeller = labeller(C = custom_c_labs))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 00:36:00