2

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]")

enter image description here

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!

markus
  • 25,843
  • 5
  • 39
  • 58
jjcii
  • 139
  • 3
  • 10
  • 1
    without any sample data, it is very difficult to help you. https://stackoverflow.com/help/minimal-reproducible-example – tjebo Jul 17 '19 at 17:29
  • Thanks. Subset of the data can be found [here](https://spaces.hightail.com/space/YbtqVilCed/files). – jjcii Jul 17 '19 at 17:56
  • 2
    For effects plots, sjPlot uses the [ggeffects-package](https://strengejacke.github.io/ggeffects), which is more customizable. See [these articles](https://strengejacke.github.io/ggeffects/articles) for examples. – Daniel Jul 21 '19 at 11:19

2 Answers2

5

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

example plot

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

re-ordered facets

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"))

desired plot

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20
  • This is fantastic, Matthew; thanks so much. I notice that replacing the color scale sets new color assignments as well. If desired, I presume I could custom set each color per usual `ggplot2` means at the same time here? Thanks again! – jjcii Jul 17 '19 at 22:39
  • 1
    Yes, you could just use the `values` argument in `scale_color_discrete()` to pick your desired colors. – the-mad-statter Jul 17 '19 at 23:03
3

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 (i.e. using sjplot's colors = "bw" argument).

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

black-and-white interaction plot

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:

interaction plot with custom legend title and labels

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.

kasia_b
  • 226
  • 2
  • 7