【发布时间】:2014-01-16 22:55:34
【问题描述】:
我正在尝试将 ggplot 中所有几何图形的默认颜色设置为黑色以外的颜色。请注意,这不是关于设置 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)
现在,假设我想让所有东西都变成红色。撇开这样做的可取性不谈,我认为ggplot(df, aes(x=x), colour="red") 会这样做。但是colour= 参数似乎被忽略了:一切仍然是黑色的。我可以将colour="red" 添加到每个geom_ 呼叫中,但我正在努力避免这种情况。
编辑:
使用ggplot(df, aes(x=x, color="red")) 不是一个选项,因为它使用默认的ggplot 调色板(evenly spaced around an HSL color circle)创建了一个色标。使用一种颜色,这是#F8766D,恰好是浅红色。此外,这会创建一个图例,然后必须将其隐藏。
【问题讨论】:
-
只是好奇,你为什么不想使用 scale_color_?
-
@PirateGrunt 问题末尾的编辑解释了它。我必须通过在 ggplot 调用中在
aes(...)中设置颜色来创建色标,然后使用scale_color_manual(values="red", guide="none")之类的东西。这只是一个 hack - 应该有一个更简单的方法。另外,假设我想对图中的其他内容使用色标,例如根据某个分组变量为点着色,并将其他所有内容设为红色。 -
@PirateGrunt 另一个原因是黑暗主题的情节。唯一的挑战是设置与几何图形相关的颜色(点、箱线图、线等)