sjPlot plot_model()中的彩带和不同的线型

时间:2019-05-09 13:32:43

标签: r ggplot2 sjplot

我想使用plot_model()包中的sjPlot来绘制具有不同线型彩色丝带的模型的预测值。我还希望线型和色带颜色显示在图例中。我确实找到了一种使用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")

1 个答案:

答案 0 :(得分:0)

也许您可以浏览功能set_theme()update_geom_defaults(),这些功能使您可以自定义绘图外观。 您只需调用一次即可。