In ggplot2 I am trying to make a scatter plot in Rstudio using a grouping variable (for the different colours of the points), however I would like to have one regression line for the data as a whole. Would be most grateful for any advice.
Asked
Active
Viewed 2,303 times
1 Answers
7
Here is an example using the iris
data set:
data(iris)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point(aes(colour = Species)) +
geom_smooth(method = "lm")
The 'trick' is to specify the grouping variable in geom_point
only, and not in the general aesthetics.
Output:

tifu
- 1,352
- 6
- 17