I am using sjPlot::plot_model
for my multiple regression plot(using lm
function).
As shown in the figure below there the points and lines in sjPlot are always dodging each other.
I have tried position_identity()
but it didn't work, I would like to know how to make them in the right place.
Recreating the same plot by ggplot2 may be too complicated bc it's a three-way interaction plot.
Thanks for your help!
You can pass dodge = 0
in your call to sjPlot::plot_model()
like so:
set.seed(42)
library(dplyr)
library(sjPlot)
library(ggplot2)
data <- tibble(
x = rep(c("A", "B"), each = 10),
y = rep(c("c", "d"), 10),
z = case_when(
x == "A" & y == "c" ~ rnorm(20, 2),
x == "A" & y == "d" ~ rnorm(20, 3),
x == "B" & y == "c" ~ rnorm(20, 1),
x == "B" & y == "d" ~ rnorm(20, 9),
TRUE ~ NA_real_
)
)
m <- lm(z ~ x*y, data)
sjPlot::plot_model(
m,
type = "pred",
terms = c("x", "y"),
dodge = 0
) +
geom_line()
Created on 2024-04-18 with reprex v2.1.0