简体   繁体   中英

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

I have successfully used the plot_model function of sjplot to plot a multinomial logistic regression model. The regression contains an outcome (Info Sought, with 3 levels) and 2 continuous predictors (DSA, ASA). I have also changed the values of ASA in the plot_model so as to plot predicted effect outcomes based on the ASA mean value and SDs:

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

在此处输入图像描述

I have two customization questions:

1) Facet Order: The facet order is based on the default alphabetical order of the outcome levels ("Expand" then "First Pic" then "Multiple Pics"). Is there a means by which to adjust this? I tried resorting the levels with factor() (as exampled here 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) Legend Labels: The legend currently labels the plotted lines with the -1 SD, mean, and +1 SD values for ASA; is there a way to adjust these labels to instead simply say "-1 SD", "mean", and "+1 SD" instead of the raw values?

Thanks!

First I replicate your plot using your supplied data:

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

样例

Your attempt to re-factor did not work because sjPlot::plot_model() does not pay heed. One way to tackle reordering the facets is to produce an initial plot as above and replace the faceting variable in the data with a factor version containing your desired order like so:

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

重新排序的方面

Finally, to tackle the legend labeling issue, we can just replace the color scale with one containing your desired labels:

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

理想的情节

Just following up on @the-mad-statter's answer, I wanted to add a note on how to change the legend title and labels when you're working with a black-and-white graph where the lines differ by linetype (ie using sjplot 's colors = "bw" argument).

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

黑白交互图

As the lines are all black, if you would like to change the axis title and labels, you need to use the scale_linetype_manual() function instead of scale_color_discrete() , like this:

p1 + scale_linetype_manual(name = "ASA values",
                           values = c("dashed", "solid", "dotted"),
                           labels = c("Low (-1 SD)", "Medium (mean)", "High (+1 SD)")) 

The resulting graph with look like this:

具有自定义图例标题和标签的交互图

Note that I also took this opportunity to change how linetypes are assigned to values, making the line corresponding to the mean of ASA solid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM