AMMD
AMMD

Reputation: 31

Neither order.terms or scale_x_discrete reorders terms in sjPlot's plot_model()

I'm trying to make some model plots using plot_model() function in the sjPlot package.

The default is to have the terms alphabetically, which isn't logical for my data (animal behavior in response to enrichments and in baselines).

The function order.terms just isn't reordering the terms, and using scale_x_discrete(limits= ...) is reordering the labels, but not their corresponding plotted data. Details below:

I initially tried using the order.terms function (based on the order of the terms in the model summary):

#model
lai<-geeglm(point.lai ~ ee2 + Observer + month,
                 data = noday3, 
                 id = ferret.id, 
                 family = binomial, 
                 corstr = "exchangeable")
#plot

plot_model(lai, type="pred", terms = c("ee2"),
            title = c(""),
            axis.title = c("EE on Day 1 and Baselines", "Probability (%) of Lying awake inattentive"),
            auto.label = F,
            order.terms = c(4,3,1,2,5,7,6))

You'll see this isn't successful: Terms aren't re-ordered using the order.terms function

I then followed the advice posted in the answer to this poster struggling with the same issue: https://stackoverflow.com/questions/66212389/order-terms-does-not-reorder-terms-in-sjplots-plot-model

which was to try using + scale_x_discrete(limits=c...) to re-order the terms:

P <- plot_model(lai, type="pred", terms = c("ee2"),
            title = c(""),
            axis.title = c("EE on Day 1 and Baselines", "Probability (%) of Lying awake inattentive"),
            auto.label = F)

P + theme_bw()+
scale_x_discrete(limits=c("bl.b","bl.a","bag", "bed", "box", "digbox", "complex"),
               labels=c("bl.b"="Baseline \n (Pre)","bl.a"="Baseline \n (Post)","bag"="Bag",       "bed"="Bed", "box"="Box", "digbox"="Dig-box", "complex"="Complex \n environment"))+
theme(axis.text.x = element_text(size=14),
    axis.text.y = element_text(size=14),
    axis.title.x = element_text(size = 16),
    axis.title.y = element_text(size = 16))

Here is the resulting plot: Labels are re-ordered by the data isn't

You'll see that the x axis labels have been reordered, but looking at the 2 graphs you'll see that the actual data hasn't been reordered.

Does anyone have any advice on how to reorder the terms and have the terms corresponding data be reordered with them?

Upvotes: 3

Views: 166

Answers (1)

Kat
Kat

Reputation: 18744

The order.terms is for when you include multiple columns. If you want to order the levels within a column, you need to make it an ordered factor. Check out my example.

The first plot has the field of education as a factor, which naturally alphabetizes the two levels: high and low.

cuse = read.table("https://data.princeton.edu/wws509/datasets/cuse.dat", 
                  header = TRUE)

cuse <- mutate(cuse, across(1:3, as.factor))

lai <- geeglm(cbind(using, notUsing) ~ age + education, id = wantsMore,
              data = cuse, family = "binomial")
plot_model(lai, type = "pred", terms = "education")

enter image description here

Now if had made this an ordered factor, and declared that low was before high, then I can switch how they appear in the plot.

cuse2 <- mutate(cuse, across(c(1, 3), as.factor), 
                education = ordered(education, c("low", "high")))

lai2 <- geeglm(cbind(using, notUsing) ~ age + education, id = wantsMore,
              data = cuse2, family = "binomial")
plot_model(lai2, type = "pred", terms = "education")

enter image description here

Upvotes: 0

Related Questions