简体   繁体   English

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

[英]Colored ribbons and different linetypes in sjPlot plot_model()

I want to use plot_model() from the sjPlot package to plot predicted values of a model with both different linetypes and colored ribbons . 我想用plot_model()sjPlot包绘制与两个不同的线型彩带模型的预测值。 I also want the linetypes and ribbon colors to show up in the legend . 我还希望线型和色带颜色显示在图例中 I did find a way to make this work using ggpredict() and ggplot(), but I would much prefer using the plot_model() function as it saves me writing a lot of code when having to fit and plot a lot of models for my projects. 我确实找到了一种使用ggpredict()和ggplot()进行这项工作的方法,但是我更喜欢使用plot_model()函数,因为它为我节省了编写和编写大量模型所需的大量代码的时间项目。

I did try two different ways shown below, but neither worked for me. 我确实尝试了如下所示的两种不同方式,但都没有用。 How can I do this? 我怎样才能做到这一点?

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")

Maybe you can explore the functions set_theme() and update_geom_defaults() that allow you to customize the plot appearance. 也许您可以探索set_theme()update_geom_defaults()函数,这些函数可用于自定义绘图外观。 You only need to call it one time. 您只需要调用一次即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM