Home > OS >  Relabelling predictors with jtools effect_plot in R
Relabelling predictors with jtools effect_plot in R

Time:11-30

I'm trying to edit the names of the factor levels of a predictor variable in R for an effect plot with the jtools package. I should be able to do this with the "pred.labels" argument, but the labels don't change. For example, using the mtcars data set I try to write out the predictor names ("four", "six", "eight") but there is no change in the figure.

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"))

Figure produced with above script, where labels have NOT been changed

Now, if this were really my problem I would just overwrite the factor levels in the data. However, in my data set I have many factors that should have a space in their name for easy interpretation, so I don't want to recode my variables this way. Any idea how to fix this?

Thanks

CodePudding user response:

Since jtools seems to use ggplot2 you can just change the labels like you would if you were using ggplot2

label <- c("four", "six", "eight")
effect_plot(mtest, pred = cyl)   scale_x_discrete(labels= label)

or

effect_plot(mtest, pred = cyl)   scale_x_discrete(labels= c("four", "six", "eight"))

example

CodePudding user response:

Not sure how to rename the group labels in jtools but you can do it with scale_x_discrete in ggplot2 since the resulting object is from ggplot.

effect_plot(mtest, pred = cyl)  
   scale_x_discrete(labels = c("four", "six", "eight"))

output

  • Related