【问题标题】:Colored ribbons and different linetypes in sjPlot plot_model()sjPlot plot_model() 中的彩色丝带和不同线型
【发布时间】:2019-05-09 13:32:42
【问题描述】:

我想使用 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")

【问题讨论】:

    标签: r ggplot2 sjplot


    【解决方案1】:

    也许您可以探索允许您自定义情节外观的函数set_theme()update_geom_defaults()。 你只需要调用一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多