英文:
How can I change the order of facets in plot_model without changing the underlying data?
问题
我使用sjPlot的plot_model函数生成了一个预测的边际效应图,该图显示了在名为"height"的变量的选定水平上的预测结果,以明确它们是-1标准差、均值和+1标准差。不幸的是,由于字母排序的原因,当"height"包括负值和正值,并且均值是一个比-1标准差更接近零的负值时,facet(子图)将不会按照前面列出的方式排序。因此,我需要以某种方式重新排序它们。由于"height"实际上是数值型的,我仅手动选择了"height"的特定值来显示在图中,因此我认为我无法通过重新排序底层数据来实现这一点,因为该变量不是因子。除非将其转换为因子并重新排序所有水平?但我希望有一种更方便的解决方案,不需要修改底层数据。
英文:
I have a predicted marginal effects plot made with plot_model from sjPlot, displaying the predictions at selected levels of the variable height which I named to clarify they are -1 SD, mean, and +1 SD, respectively. Unfortunately, due to alphabetical ordering, when height includes both negative and positive values and the mean is a negative value closer to zero than -1 SD, the facets will not be ordered in the way listed before. Thus, I have to reorder them somehow. Since height is actually numeric and I only manually selected specific values of height to display in the plot, I don't see that I could achieve this by reordering the underlying data, since the variable is not a factor. Except maybe converting it to a factor and reordering all the levels? But I hope there's a more convenient solution that doesn't require modifying the underlying data.
library(dplyr)
library(sjPlot)
#specifying the model
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])
#plotting predictions at three specified levels of height, let's pretend they're -1 SD, mean and +1 #SD
plot = plot_model(model, type = "pred", terms = c("species", "gender", "height [-2, -1, 1]"), ci.lvl=0.95)
#renaming facets to specify that they represent -1 SD, mean, and +1 SD
plot$data$facet <- ifelse(plot$data$facet == "height = -2", "height = -2 (-1 SD)",
ifelse(plot$data$facet == "height = -1", "height = -1 (mean)",
ifelse(plot$data$facet == "height = 1", "height = 1 (+1 SD)", 99)))
答案1
得分: 2
你可以通过将数据中的facet
变量转换为具有所需顺序的因子来实现这一点。
library(dplyr)
library(sjPlot)
# 指定模型
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])
# 在三个指定的身高水平上绘制预测,假设它们是 -1 SD、平均和 +1 SD
plot = plot_model(model, type = "pred", terms = c("species", "gender", "height [-2, -1, 1]"), ci.lvl=0.95)
#> 在预测的时候出现警告: prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases
# 重新设置facet变量的水平顺序
plot$data$facet <- factor(case_when(
plot$data$facet == "height = -2" ~ "height = -2 (-1 SD)",
plot$data$facet == "height = -1" ~ "height = -1 (mean)",
plot$data$facet == "height = 1" ~ "height = 1 (+1 SD)",
), levels=c("height = -2 (-1 SD)", "height = -1 (mean)", "height = 1 (+1 SD)"))
plot
<!-- -->
<sup>创建于2023-06-22,使用 reprex v2.0.2</sup>
英文:
You can do this by making the facet
variable in the data a factor with the levels in the order you want.
library(dplyr)
library(sjPlot)
#specifying the model
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])
#plotting predictions at three specified levels of height, let's pretend they're -1 SD, mean and +1 #SD
plot = plot_model(model, type = "pred", terms = c("species", "gender", "height [-2, -1, 1]"), ci.lvl=0.95)
#> Warning in predict.lm(model, newdata = data_grid, type = "response", se.fit =
#> se, : prediction from rank-deficient fit; attr(*, "non-estim") has doubtful
#> cases
plot$data$facet <- factor(case_when(
plot$data$facet == "height = -2" ~ "height = -2 (-1 SD)",
plot$data$facet == "height = -1" ~ "height = -1 (mean)",
plot$data$facet == "height = 1" ~ "height = 1 (+1 SD)",
), levels=c("height = -2 (-1 SD)", "height = -1 (mean)", "height = 1 (+1 SD)"))
plot
<!-- -->
<sup>Created on 2023-06-22 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论