使用 plot_model 根据随机效应改变数据点的颜色
Changing colour of data points according to random effect using plot_model
我想在混合效应模型中绘制响应变量和解释变量之一之间的回归,同时还显示数据点。为此,我使用函数 plot_model()
。我还希望数据点根据随机效果具有不同的颜色或形状。我不想为随机效应的每个级别绘制不同的回归线。 plot_model
这可能吗?
这是数据:
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")
这是混合效果模型:
MEM <- lmer(formula = SpR ~ (1|Yr) +
Dep + Sal + Chla + Ice +
Dep*Ice,
data = dat)
这是我用来绘制 SpR 和 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)
这是我从 运行 这段代码中得到的数字。我希望圆点根据年份有不同的颜色:
如果您在 plot_model()
调用中设置 show.data = FALSE
,然后单独添加点,它似乎工作正常:
MEMpld +
geom_point(data=dat, aes(x=Dep, y=SpR, colour = Yr)) +
scale_colour_discrete(name = "Yr") +
guides(fill="none")
我不确定为什么这个图例看起来有点古怪;我试图关闭填充图例(我认为这是内部设置的),但它似乎没有达到我想要的效果。
我想在混合效应模型中绘制响应变量和解释变量之一之间的回归,同时还显示数据点。为此,我使用函数 plot_model()
。我还希望数据点根据随机效果具有不同的颜色或形状。我不想为随机效应的每个级别绘制不同的回归线。 plot_model
这可能吗?
这是数据:
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")
这是混合效果模型:
MEM <- lmer(formula = SpR ~ (1|Yr) +
Dep + Sal + Chla + Ice +
Dep*Ice,
data = dat)
这是我用来绘制 SpR 和 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)
这是我从 运行 这段代码中得到的数字。我希望圆点根据年份有不同的颜色:
如果您在 plot_model()
调用中设置 show.data = FALSE
,然后单独添加点,它似乎工作正常:
MEMpld +
geom_point(data=dat, aes(x=Dep, y=SpR, colour = Yr)) +
scale_colour_discrete(name = "Yr") +
guides(fill="none")
我不确定为什么这个图例看起来有点古怪;我试图关闭填充图例(我认为这是内部设置的),但它似乎没有达到我想要的效果。