首页 > 解决方案 > 在包 sjPlot::plot_model() 中定义 y 轴范围和中断

问题描述

我正在使用 sjplot 包和 R 中的函数 plot_model (max.m3) 绘制 GlmmTMB 模型的结果。这是代码:

p=sjPlot::plot_model(max.m3, type="pred", grid = F)

这些是绘制的六个图形。但是,我想定义 y 轴范围(范围从 0 到 10)并显示中断(0、5、10 = 以便刻度线出现在 0、5 和 10 处)。

不幸的是,我没有找到解决方案。

标签: r

解决方案


如果为所有模型项绘制边际效应,则plot_model()返回ggplot 对象列表。然后,您可以简单地使用 ggplot-commands 修改列表中的每个图。

m <- lm(mpg ~ hp + gear + cyl + drat, data = mtcars)
p <- sjPlot::plot_model(m, type = "pred", grid = FALSE)
p[[1]] + scale_y_continuous(limits = c(15, 30), breaks = c(15, 25, 30))
p[[2]] + scale_y_continuous(limits = c(5, 40), breaks = c(15, 25, 40))
...

如果您想对所有绘图应用相同的 y 限制和中断,您可以遍历列表,例如:

library(ggplot2)
m <- lm(mpg ~ hp + gear + cyl + drat, data = mtcars)
p <- sjPlot::plot_model(m, type = "pred", grid = FALSE)
lapply(p, function(i) i + scale_y_continuous(limits = c(15, 30), breaks = c(15, 25, 30)))

推荐阅读