You can share the variables inside mapping between the geoms. Specifically about color
, it needs to be defined outside of the aes
and in the geom
when setting to a constant (e.g. "blue"
) and not a variable (explained here: Answer to: When does the argument go inside or outside aes).
I included couple examples below to illustrate this better;
library(ggplot2)
## works fine for each geom
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(color = "blue") +
geom_line(color="red")

## doesn't work when not in the geom
ggplot(data = mpg, aes(x = displ, y = hwy), color = "blue") +
geom_point()

## gets evaluated as a variable when in aes
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = "blue"))

## can use a variable in aes, either in geom or ggplot function
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = model), show.legend = FALSE)
