ModalBro
ModalBro

Reputation: 554

Changing the label for an interaction term when plotting a regression with SjPlot

I am trying to use sjPlot to plot the coefficients of a regression that includes several interaction terms. I have changed the labels of all the variables so that they're more intelligible, but unfortunately, the interactions show up as just variable_1:variable_2.

Is there a way to modify the output for these interactions so it would display as "label_1 x label_2" or something to that effect?

Here's an example:

data(mtcars)

library(ggplot2)
library(sjPlot)
library(sjlabelled)
library(dplyr)

mtcars <- mtcars %>% var_labels(
  mpg = "Miles per Gallon",
  cyl = "Cylinder"
)

x <- lm(hp ~ mpg*cyl, data=mtcars)

plot_model(x)

Upvotes: 0

Views: 1606

Answers (1)

Edward
Edward

Reputation: 18683

You can add the labels within the scale_x_discrete function. Doing this obviates the need to add var_labels to the variables.

data(mtcars)
x <- lm(hp ~ mpg*cyl, data=mtcars) 

plot_model(x) +
  scale_x_discrete(labels=list(
    mpg = "Miles per Gallon", 
    cyl = "Cylinders",
    `mpg:cyl` = "Miles per Gallon : Cylinders"))

enter image description here

Upvotes: 1

Related Questions