我正在尝试将ggplot中所有geom的默认颜色设置为黑色以外的其他颜色.请注意,这不是关于设置scale_color ...
简单的例子:
# linear model with confidence bands...
set.seed(1)
df <- data.frame(x=1:50, y=5 + 2*(1:50)+rnorm(50,sd=10))
lm <- lm(y~x,df)
se <- summary(lm)$sigma # standard error of fit
Z <- qnorm(0.05/2,lower.tail=F) # 95% confidence bands
df <- cbind(df,predict(lm,se.fit=T)[c("fit","se.fit")])
# plot the result...
library(ggplot2)
ggplot(df, aes(x=x)) +
geom_point(aes(y=y), size=3) +
geom_line(aes(y=fit)) +
geom_line(aes(y=fit+Z*se.fit), linetype=2)+
geom_line(aes(y=fit-Z*se.fit), linetype=2)
Run Code Online (Sandbox Code Playgroud)
现在,假设我想让一切都变红.撇开这样做的可取性,我认为ggplot(df, aes(x=x), colour="red")会这样做.但这个colour=参数似乎被忽略了:一切都还是黑的.我可以添加colour="red"到每个geom_电话,但我试图避免这种情况.
编辑:
使用ggplot(df, aes(x=x, color="red"))不是一个选项,因为它使用默认的ggplot调色板(围绕HSL色环均匀分布)创建色阶.只有一种颜色,恰好是浅红色.此外,这会创建一个必须隐藏的图例.#F8766D