[英]order.terms does not reorder terms in sjPlot's plot_model
I have code to graph a simple three-level one factor regression and I can't convince sjPlot to reorder the terms on the X-axis and I wondered if anyone could help me figure out what is going on.我有代码来绘制一个简单的三级单因子回归图,但我无法说服 sjPlot 对 X 轴上的项重新排序,我想知道是否有人可以帮助我弄清楚发生了什么。
My code:我的代码:
m0 <- lmer(ans ~ type + (1|subject/target), data=behavioral_data)
summary(m0)
p1 <- plot_model(m0,
type = "pred",
terms = c("type"),
order.terms = c(2, 1),
auto.label = F,
title = "Model Estimates of Answer (Marginal Effects)",
axis.title = c("Target Type", "Answer")
)
The output the model summary produces: output model 摘要产生:
Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: ans ~ type + (1 | subject/target)
Data: behavioral_data
REML criterion at convergence: 15354
Scaled residuals:
Min 1Q Median 3Q Max
-2.8944 -0.7136 -0.1561 0.6646 3.2381
Random effects:
Groups Name Variance Std.Dev.
target:subject (Intercept) 0.1434 0.3787
subject (Intercept) 0.3051 0.5524
Residual 1.7003 1.3040
Number of obs: 4447, groups: target:subject, 444; subject, 37
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 2.74088 0.10181 48.14515 26.922 <2e-16 ***
typeN -0.03277 0.06509 404.96582 -0.503 0.6149
typeY -0.14263 0.06506 404.00056 -2.193 0.0289 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The graph I get:我得到的图表:
I expected order.terms = c(2, 1)
to reorder Y and N. What am I missing?我希望order.terms = c(2, 1)
重新排序 Y 和 N。我错过了什么?
Since what plot_model() turned back is a ggplot-object, you could add other ggplot-related functions to post modify the plot.由于 plot_model() 返回的是一个 ggplot 对象,您可以添加其他与 ggplot 相关的函数来后期修改 plot。 For example, try this:例如,试试这个:
p1 <- plot_model(m0,
type = "pred",
terms = c("type"),
order.terms = c(2, 1),
auto.label = F,
title = "Model Estimates of Answer (Marginal Effects)",
axis.title = c("Target Type", "Answer")
) + scale_x_discrete(limits=c("S", "Y", "N"))
The limits=() argument in the discrete-series could accept a vector of characters to indicate the order of the x-axis.离散系列中的 limits=() 参数可以接受一个字符向量来指示 x 轴的顺序。
Alternatively, you could apply a fct_relevel() function from the forcats package, which will order the factors in the same way for all future regressions.或者,您可以应用 forcats package 中的 fct_relevel() function,它将以相同的方式对所有未来回归的因素进行排序。 Applying this function to the original dataset before running the regression and the plot_model function solves this issue.在运行回归之前将此 function 应用于原始数据集,plot_model function 解决了此问题。
Example:例子:
data2 <- data %>%
mutate(name = fct_relevel(name,
"north", "north-east", "east",
"south-east", "south", "south-west",
"west", "north-west"))
Now, when you apply the data.table to regression and plot using plot_model, the factors will show up in the order above.现在,当您将 data.table 应用于回归并使用 plot_model 将 plot 应用于回归时,因子将按上述顺序显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.