首页 > 解决方案 > 如何在 R 中使用 ggPredict 更改图形的颜色?情节(b,颜色=“bw”)?

问题描述

我做了这个模型:

lm1 <- lm(respiration.rate.calf~length+abs_pres,data=data)

并尝试将其可视化ggPredict

b<-ggPredict(lm1, se=TRUE)+labs(x="Calf length (m)", y="Calf respiration rate (breaths/min)")
plot(b)

但是,我得到的图表是红色和绿色的,但我必须以黑白打印图表。有谁知道如何更改线型(例如,一种实线和一种虚线)或颜色(一种亮和一种暗)ggPredict

我现在已经尝试了 3 个小时,我尝试的最后一件事是这样,但它不起作用:

plot(b, colors="bw")

标签: rgraphcolors

解决方案


您可以使用ggplot()功能并根据需要更改颜色

library(ggeffects)
library(ggplot2)
data(efc)
fit <- lm(barthtot ~ c12hour + bs(neg_c_7) * c161sex + e42dep, data = efc)

ggpredict(fit, terms = "c12hour")
mydf <- ggpredict(fit, se=TRUE)
mydf <- ggpredict(fit, terms = c("neg_c_7", "c161sex", "e42dep"))

ggplot(mydf, aes(x = x, y = predicted, colour = group)) +
  geom_line() +
  geom_ribbon( aes(ymin = conf.low, ymax = conf.high, fill = group, color = NULL), alpha = .15) +
  facet_wrap(~facet) +
  theme_bw() +
  scale_color_manual(values=c('#999999','#E69F00'))+
  scale_fill_manual(values=c("blue", "green"), name="fill")

在此处输入图像描述


推荐阅读