如何在使用 sjPlot

问题描述

我使用 mtcars 的数据构建了一个 lmer 模型,如下所示。 问题是,当我使用 sjplot::plot_model 绘制边际效应图时,我发现图例的顺序颠倒了。预期的顺序是低值在底部,大值在顶部。你能帮我解决吗?感谢所有关注此问题的人。

mdl <- lmer(disp~mpg:wt+vs+(1|cyl),data = mtcars)

sjplot::plot_model(mdl,type = 'pred',terms = c("mpg [all]","wt"))

enter image description here

解决方法

由于您基本上是在处理 ggplot,因此您可以通过 guide_legend 颠倒图例的顺序:

library(lme4)
#> Loading required package: Matrix
library(ggplot2)

mdl <- lmer(disp~mpg:wt+vs+(1|cyl),data = mtcars)

sjPlot::plot_model(mdl,type = 'pred',terms = c("mpg [all]","wt")) +
  guides(color = guide_legend(reverse = TRUE))