Reputation: 1440
The following code makes each group have a different colored point on a scatterplot.
df = mtcars
df$cyl = factor(df$cyl)
ggplot(df, aes(x = mpg, y = disp, colour = cyl, group = cyl)) +
geom_point(size = 5) +
geom_smooth()
However, when I add a smooth line - a smoothed line is created for each group. How do I add a smoothed line for all the data, while retaining the different group colors?
Upvotes: 2
Views: 987
Reputation: 15143
change location of colour and group will do
df %>%
ggplot( aes(x = mpg, y = disp)) +
geom_point(size = 5, aes(colour = cyl, group = cyl)) +
geom_smooth()
Upvotes: 3