Reputation: 3
I have a graph with points from multiple levels of a categorical variable and the relevant loess smoothing lines for each level of the same variable. I need to edit BOTH the points AND the lines as in order convert the default colour graph to a meaningful black and white graph. I can edit the points using scale_shape_manual but not the lines. The following code uses mtcars to get close to what I need. Unfortunately all the liens are the same color (red)I suspect I don't understand where these sorts of ?aesthetic? commands should be placed. Any assistance greatly appreciated.
c <- ggplot(mtcars, aes(y=wt, x=mpg, shape=factor(cyl)))
c +
stat_smooth(method=loess, size = 1, col="red") +
geom_point(aes(fill = factor(cyl)), size = 4) +
scale_shape_manual(values=c(16,22, 1))
Upvotes: 0
Views: 445
Reputation: 13803
There are a few ways select specific colors for discrete and gradient scales in ggplot2
, mostly centering around usage of the scale_color_...
and scale_fill_...
functions here in a similar way to how you have used scale_shape_manual()
.
Since you're looking for grayscale, it's probably most straightforward to having those scales selected automatically for you using the convenience functions scale_color_grey()
and scale_fill_grey()
.
Finally, in order to have ggplot2
select the color of each line, you need to have the legend created for that and place the color=
aesthetic within aes()
. You had col=
(another way to indicate color=
) outside of aes()
for stat_smooth()
, so all lines were drawn with that color. I believe this is what you are looking to do:
ggplot(mtcars, aes(y=wt, x=mpg, shape=factor(cyl))) +
stat_smooth(method=loess, size = 1, aes(color=factor(cyl))) +
geom_point(aes(fill = factor(cyl)), size = 4) +
scale_shape_manual(values=c(16,22, 1)) +
scale_color_grey() + scale_fill_grey()
You'll notice it's hard to see the lightest line color. You can fix that by applying a bit of transparency to the fill around geom_smooth()
via alpha=
and also changing to the theme_bw()
to have a white background:
ggplot(mtcars, aes(y=wt, x=mpg, shape=factor(cyl))) + theme_bw() +
stat_smooth(method=loess, size = 1, aes(color=factor(cyl)), alpha=0.18) +
geom_point(aes(fill = factor(cyl)), size = 4) +
scale_shape_manual(values=c(16,22, 1)) +
scale_color_grey() + scale_fill_grey()
Upvotes: 1