I want to plot the regression between a response variable and one of the explanatory variables in a mixed effects model while also showing the data points. For this I am using the function plot_model()
. I would also like the data points to have different colours or shapes according to the random effect. I do not want to plot the different regression lines for each level of the random effect. Is this possible with plot_model
?
This is the data:
dat <- data.frame(c(1, 1, 1, 1, 1, 1, 1, 4, 1, 2, 1, 2, 3, 1,
5, 1, 2, 2, 1, 1, 2, 2, 4, 3, 5, 5, 4, 4,
8, 4, 13, 8, 6, 5, 6, 5, 4, 8, 6, 2, 3, 9, 7,
4, 8, 8, 10, 10, 3, 3),
c(427.0, 110.0, 99.0, 450.0, 182.0, 34.0, 43.0, 105.0,
22.0, 108.0, 85.0, 142.0, 81.0, 109.0, 89.0, 183.0,
106.0, 19.3, 13.5, 36.0, 55.0, 36.0, 128.0, 38.2, 103.0,
105.0, 91.0, 94.0, 106.0, 96.0, 108.0, 103.0, 122.0,
103.0, 112.0, 190.0, 112.0, 77.0, 60.0, 148.0, 161.0,
44.0, 188.0, 44.0, 88.0, 90.0, 124.0, 120.0, 212.0, 309.0),
c(32.3, 27.0, 26.7, 33.0, 28.4, 26.7, 28.0, 30.0, 26.5,
28.4, 29.0, 30.4, 25.8, 26.4, 24.2, 28.6, 29.4, 27.7,
25.5, 27.3, 27.4, 29.2, 21.5, 25.4, 27.8, 27.8, 30.2,
30.2, 30.9, 30.0, 30.3, 31.5, 31.3, 31.1, 32.5, 32.0,
32.2, 32.9, 32.9, 31.3, 32.3, 29.4, 31.5, 29.8, 26.7,
26.7, 29.9, 29.9, 32.1, 32.1),
c(7.73, 1.83, 2.97, 7.66, 1.80, 1.17, 0.66, 6.04, 1.72,
1.25, 2.27, 1.65, 0.16, 0.16, 0.26, 0.11, 0.17, 0.15,
0.20, 0.21, 0.15, 0.10, 1.83, 1.84, 7.67, 7.67, 5.81,
5.81, 3.95, 3.45, 1.15, 3.07, 3.61, 3.35, 0.86, 2.20,
4.31, 1.64, 1.14, 3.55, 4.81, 1.36, 1.94, 3.36, 3.30,
3.30, 2.66, 2.66, 6.55, 6.55),
c(94, 151, 111, 94, 87, 128, 126, 39, 130, 107, 93,
110, 76, 309, 38, 19, 13, 113, 110, 108, 103, 30,
58, 151, 55, 55, 42, 42, 29, 37, 20, 20, 61,
21, 3, 8, 28, 33, 32, 12, 38, -7, -5, 74,
22, 22, 10, 10, 22, 22),
c("2005", "2005", "2005", "2005", "2005", "2005", "2005",
"2018", "2005", "2005", "2005", "2005", "2010", "2010",
"2010", "2010", "2010", "2010", "2010", "2010", "2010",
"2010", "2012", "2012", "2017", "2017", "2017", "2017",
"2017", "2017", "2017", "2017", "2017", "2017", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018", "2018", "2018", "2018", "2018", "2018", "2018",
"2018"))
colnames(dat) <- c("SpR", "Dep", "Sal", "Chla", "Ice", "Yr")
This is the mixed effects model:
MEM <- lmer(formula = SpR ~ (1|Yr) +
Dep + Sal + Chla + Ice +
Dep*Ice,
data = dat)
This is the code I'm using to plot the regression between SpR and Dep:
MEMpld <- plot_model(MEM,
type = "pred",
pred.type = "fe",
terms = c("Dep"),
show.data = TRUE,
col = "black",
title = "",
jitter = 0.0002,
dot.size = 3.5,
dot.alpha=1)
This is the figure that I get from running this code. I would like the dots to have different colours according to the year: