For example
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(color = Species)) +
geom_smooth(aes(color = Species, fill = Species), method = "lm", se = FALSE)
Is there some way to assign a palette (for example through scale_color_discrete
) to points and a different paler to the lines produced by geom_smooth
?
Thank you!
There isn't a very clean way to do this within ggplot2
, but there is a package called ggnewscale
that will help.
library(ggplot2)
library(ggnewscale)
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(color = Species)) +
scale_colour_brewer(palette = 1) +
ggnewscale::new_scale_color() +
geom_smooth(aes(color = Species), method = "lm", se = FALSE) +
scale_colour_brewer(palette = 2)
#> `geom_smooth()` using formula = 'y ~ x'
Created on 2024-07-06 with reprex v2.1.1
Another solution without any new packages, is just changing the color
to fill
and giving the points a shape.
library(ggplot2)
library(ggnewscale)
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_smooth(aes(color = Species), method = "lm", se = FALSE) +
geom_point(aes(fill = Species), shape = 21, size = 3, colour="transparent") +
scale_colour_brewer(palette = 2) +
scale_fill_brewer(palette = 1)
#> `geom_smooth()` using formula = 'y ~ x'
Created on 2024-07-06 with reprex v2.1.1