I'm trying to set the default color for all geoms in a ggplot to something other than black. Note this is not about setting scale_color...
Simple example:
# 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)
Now, suppose I want to make everything red. Leaving aside the advisability of doing that, I would think ggplot(df, aes(x=x), colour="red")
would do it. But the colour=
parameter seems to be ignored: everything is still black. I can add colour="red"
to every geom_
call, but I'm trying to avoid that.
Edit:
Using ggplot(df, aes(x=x, color="red"))
is not an option because it creates a color scale using the default ggplot palette (evenly spaced around an HSL color circle). With one color, this is #F8766D
, which happens to be light red. In addition, this creates a legend which then must be hidden.
aes(...)
in the ggplot call, then use something likescale_color_manual(values="red", guide="none")
. It's just a hack - there should be an easier way. Plus, suppose I want to use a color scale for something else in the plot, say color the points based on some grouping variable and make everything else red. – Spoliate