在 R 中使用 jtools effect_plot 重新标记预测变量
Relabelling predictors with jtools effect_plot in R
我正在尝试使用 jtools 包为效果图编辑 R 中预测变量的因子水平的名称。我应该可以使用“pred.labels”参数来做到这一点,但标签不会改变。例如,使用 mtcars 数据集,我尝试写出预测变量名称(“四”、“六”、“八”),但图中没有任何变化。
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtest <- lm(mpg ~ cyl + hp + wt, data = mtcars)
effect_plot(mtest, pred = cyl, pred.labels = c("four", "six", "eight"))
现在,如果这真的是我的问题,我会覆盖数据中的因子水平。但是,在我的数据集中,我有很多因素,它们的名称中应该有一个 space 以便于解释,所以我不想以这种方式重新编码我的变量。知道如何解决这个问题吗?
谢谢
由于 jtools
似乎使用 ggplot2
,您可以像使用 ggplot2
一样更改标签
label <- c("four", "six", "eight")
effect_plot(mtest, pred = cyl) + scale_x_discrete(labels= label)
或
effect_plot(mtest, pred = cyl) + scale_x_discrete(labels= c("four", "six", "eight"))
不确定如何在 jtools 中重命名组标签,但您可以在 ggplot2 中使用 scale_x_discrete
来完成,因为生成的对象来自 ggplot。
effect_plot(mtest, pred = cyl) +
scale_x_discrete(labels = c("four", "six", "eight"))
我正在尝试使用 jtools 包为效果图编辑 R 中预测变量的因子水平的名称。我应该可以使用“pred.labels”参数来做到这一点,但标签不会改变。例如,使用 mtcars 数据集,我尝试写出预测变量名称(“四”、“六”、“八”),但图中没有任何变化。
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtest <- lm(mpg ~ cyl + hp + wt, data = mtcars)
effect_plot(mtest, pred = cyl, pred.labels = c("four", "six", "eight"))
现在,如果这真的是我的问题,我会覆盖数据中的因子水平。但是,在我的数据集中,我有很多因素,它们的名称中应该有一个 space 以便于解释,所以我不想以这种方式重新编码我的变量。知道如何解决这个问题吗?
谢谢
由于 jtools
似乎使用 ggplot2
,您可以像使用 ggplot2
label <- c("four", "six", "eight")
effect_plot(mtest, pred = cyl) + scale_x_discrete(labels= label)
或
effect_plot(mtest, pred = cyl) + scale_x_discrete(labels= c("four", "six", "eight"))
不确定如何在 jtools 中重命名组标签,但您可以在 ggplot2 中使用 scale_x_discrete
来完成,因为生成的对象来自 ggplot。
effect_plot(mtest, pred = cyl) +
scale_x_discrete(labels = c("four", "six", "eight"))