0

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.

Bartlett
  • 9
  • 2

1 Answers1

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:

enter image description here

tifu
  • 1,352
  • 6
  • 17