简体   繁体   中英

Reorder x axis using plot_model() from sjPlot

I have run a binomial logistic regression model in R using the lme4 package. Now, I want to plot the estimated marginal means for the model, so I have installed the sjPlot package and I have used the plot_model() function.

My x axis includes three variables corresponding to three different groups: "L1", "HS", and "L2". I want to have the three variables in that precise order. However, when I plot the model, I get "HS" before "L1", because the labels appear in alphabetical order. I would like to change the order of those two labels and I know how to do that in a dataframe, but not when plotting a model with that function. Any ideas on how to reorder my x axis using the sjPlot package?

You can change the order of the coefficients using the order.terms -argument. Note that the numbers for this argument correspond to the position of the summary. Example:

library(sjPlot)
library(sjlabelled)
data(efc)
efc <- as_factor(efc, c161sex, e42dep, c172code)
m <- lm(neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, data = efc)

plot_model(m, auto.label = F)


summary(m)
#> 
#> Call:
#> lm(formula = neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, 
#>     data = efc)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6.5411 -2.0797 -0.5183  1.3256 19.1412 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 17.65938    0.82864  21.311  < 2e-16 ***
#> pos_v_4     -0.66552    0.05163 -12.890  < 2e-16 ***
#> c12hour      0.01134    0.00270   4.201 2.95e-05 ***
#> e42dep2      0.84189    0.47605   1.768 0.077355 .  
#> e42dep3      1.73616    0.47118   3.685 0.000244 ***
#> e42dep4      3.10107    0.50470   6.144 1.26e-09 ***
#> c172code2    0.12894    0.28832   0.447 0.654844    
#> c172code3    0.69876    0.36649   1.907 0.056922 .  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 3.27 on 810 degrees of freedom
#>   (90 observations deleted due to missingness)
#> Multiple R-squared:  0.2981, Adjusted R-squared:  0.292 
#> F-statistic: 49.15 on 7 and 810 DF,  p-value: < 2.2e-16

# according to summary, order of coefficients:
# 1=pos_v_4, 2=c12hour, 3=e42dep2, 4=e42dep3, ...
plot_model(m, auto.label = F, order.terms = c(1,2,4,5,3,6,7))

Created on 2019-05-08 by the reprex package (v0.2.1)

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