我手动将图例添加到ggplot2
,并希望更改与图例关联的颜色。例如:
require(ggplot2)
set.seed(123)
dat <- data.frame(x = rnorm(100), z = rnorm(100), y1 = rnorm(100), y2 = rnorm(100))
dat1 <- dat[,c(1,2,3)]
mod1 <- lm(y1 ~ x + z, data = dat1)
dat1$mod1 <- predict(mod1, newdata =dat1)
err <- predict(mod1, newdata =dat1, se = TRUE)
dat1$ucl <- err$fit + 1.96 * err$se.fit
dat1$lcl <- err$fit - 1.96 * err$se.fit
var<- "Var"
col <- "green"
ggplot(dat1, aes(x = x)) +
geom_point(aes(x = x, y = mod1, fill = "X"), size = .8, colour = "red") +
geom_smooth(data = dat1, aes(x= x, y = mod1, ymin = lcl, ymax = ucl), size = 1, colour = "blue", se = TRUE, stat = "smooth", method = "lm")+
scale_fill_manual(name = "Legend", limits = var, values = col)
使用此代码,图例保持红色而不是绿色。知道为什么会发生这种情况以及如何解决这个问题吗?谢谢。