I am trying to plot an interaction plot using the sjPlot
package with the following code:
model <-lm(score ~ var1 + var2 + var3 + var4 + var1*var2,data=mydata)
plot1 <- plot_model(model,type="int",ci.lvl=NA, title="", legend.title="",
axis.title=c("var2","score"))
The data I'm using is similar to this dataframe:
n <- 50
mydata<- data.frame(
ID = 1:n,
var1 = sample(c("rank1", "rank2", "rank3"), n, replace = TRUE),
var2 = sample(0:2, n, replace = TRUE),
var3 = runif(n, min = 1, max = 60),
var4 = sample(c(0, 1), n, replace = TRUE)
)
Here are the first 10 rows:
ID var1 var2 var3 var4
1 1 rank2 0 11.252379 1
2 2 rank1 1 29.954015 0
3 3 rank2 2 42.397683 1
4 4 rank1 1 12.917567 1
5 5 rank3 1 8.095043 1
6 6 rank1 0 25.986603 0
7 7 rank3 2 10.349064 0
8 8 rank1 1 35.423613 1
9 9 rank2 0 24.232398 1
10 10 rank1 2 12.996389 1
var1
has 3 categories (rank1, rank2, rank3), so the interaction plot has 3 lines (score
on the y-axis and var2
on the x-axis). I want to remove the line representing rank2 only, so the remaining plot will only have 2 lines.
TYIA
I used type="pred"
rather than type="int"
because it allows better control. You can then specify which is the term for the x
axis, and which term (at which levels) to use as the moderator. See below:
plot_model(model,
type="pred",
ci.lvl=NA,
title="",
legend.title="",
axis.title=c("var2","score"),
terms=c("var2","var1 [rank2,rank3]")
)