使用 sjplot 的 plot_model 函数时调整刻面顺序和图例标签

Adjusting facet order and legend labels when using plot_model function of sjplot

我已经成功地使用 sjplotplot_model 函数来绘制多项逻辑回归模型。回归包含一个结果(寻求信息,具有 3 个水平)和 2 个连续预测变量(DSA、ASA)。我还更改了 plot_model 中 ASA 的值,以便根据 ASA 平均值和 SD 绘制预测效果结果:

plot1 <- plot_model(multinomialmodel , type = "pred", terms = c("DSA", "ASA[meansd]")

我有两个定制问题:

1) 分面顺序:分面顺序基于结果级别的默认字母顺序("Expand" 然后 "First Pic" 然后 "Multiple Pics")。有没有办法调整这个?我尝试使用 factor() 来计算关卡(例如 with ggplot2) prior to running and plotting the model, but this did not cause any changes in the resulting facet order. Perhaps instead something through ggplot2, as exampled in the first solution provided here?

2) 图例标签:图例当前用 ASA 的 -1 SD、平均值和 +1 SD 值标记绘制的线;有没有办法调整这些标签,而不是简单地说“-1 SD”、"mean" 和“+1 SD”而不是原始值?

谢谢!

首先,我使用您提供的数据复制了您的情节:

library(dplyr)
library(readr)
library(nnet)
library(sjPlot)

"ASA,DSA,Info_Sought
-0.108555801,0.659899854,First Pic
0.671946671,1.481880373,First Pic
2.184170211,-0.801398848,First Pic
-0.547588442,1.116555698,First Pic
-1.27930951,-0.299077419,First Pic
0.037788412,1.527545958,First Pic
-0.74271406,-0.755733264,Multiple Pics
1.20854212,-1.166723523,Multiple Pics
0.769509479,-0.390408588,Multiple Pics
-0.450025633,-1.02972677,Multiple Pics
0.769509479,0.614234269,Multiple Pics
0.281695434,0.705565438,Multiple Pics
-0.352462824,-0.299077419,Expand
0.671946671,1.481880373,Expand
2.184170211,-0.801398848,Expand
-0.547588442,1.116555698,Expand
-0.157337206,1.070890114,Expand
-1.27930951,-0.299077419,Expand" %>% 
  read_csv() -> d

multinomialmodel <- multinom(Info_Sought ~ ASA + DSA, data = d)

p1 <- plot_model(multinomialmodel , 
                 type = "pred", 
                 terms = c("DSA", "ASA[meansd]"))
p1

您尝试 re-factor 没有成功,因为 sjPlot::plot_model() 没有注意。解决分面重新排序的一种方法是生成一个如上所述的初始图,并将数据中的分面变量替换为包含所需顺序的因子版本,如下所示:

p2 <- p1
p2$data$response.level <- factor(p2$data$response.level, 
                                 levels = c("Multiple Pics", "First Pic", "Expand"))
p2

最后,为了解决图例标签问题,我们可以将色标替换为包含您所需标签的色标:

p2 + 
  scale_color_discrete(labels = c("-1 SD", "mean", "+1 SD"))