我想使用sjPlot包中的plot_model()来绘制具有不同线型和彩色色带的模型的预测值。我还希望线型和色带颜色显示在图例中。我确实找到了一种使用 ggpredict() 和 ggplot() 来完成这项工作的方法,但我更喜欢使用 plot_model() 函数,因为当我不得不为我的模型拟合和绘制大量模型时,它可以节省我编写大量代码的时间项目。
我确实尝试了下面显示的两种不同的方法,但都不适合我。我怎样才能做到这一点?
library("ggplot2")
library("ggeffects")
library("sjPlot")
data(efc)
fit <- lm(neg_c_7 ~ c12hour * barthtot, data = efc)
# Preferred output
ggpredict(fit, terms = c("c12hour", "barthtot")) %>%
ggplot(aes(x, predicted)) +
geom_ribbon(aes(
ymin = conf.low,
ymax = conf.high,
fill = group
),
alpha = 0.5
) +
geom_line(aes(linetype = group)) +
scale_linetype_manual(values = c("dotted", "dashed", "solid")) +
labs(
title = "Predicted values of Negative impact with 7 items",
x = "average number of hours of care per week",
y = "Negative impact with 7 items",
fill = "Total score BARTHEL INDEX",
linetype = "Total score BARTHEL INDEX"
)
# First try: Changing the linetype does not seem to work
plot_model(fit,
type = "pred",
terms = c("c12hour", "barthtot")
) +
scale_linetype_manual(values = c("dotted", "dotdash", "longdash"))
# Second try: With colors = "bw" I can change the linetype, and with
# scale_fill_brewer() I can add colored ribbons, but the legend does
# not show the colors
plot_model(fit,
type = "pred",
terms = c("c12hour", "barthtot"),
colors = "bw"
) +
scale_linetype_manual(values = c("dotted", "dotdash", "longdash")) +
scale_fill_brewer(palette = "Set1")