Search code examples
rggplot2linear-regressionhue

Do linear regression with all points not separated by hue


library(ggplot2)

ggplot(df, aes(x = x, y = y, color = category)) +
    geom_point() +  
    geom_smooth(method = "lm", color = "black")    

This does the linear regression separately for each category. I would like to color the points differently, but use ALL the points for the linear regression (just one linear regression).

What can I change to achieve this?


Solution

  • To generate one regression line but still have the points colored you can pass color to geom_point instead of globally

    library(ggplot2)
    library(palmerpenguins)
    
    
    ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
      geom_point(aes(color = species)) +
      geom_smooth(method = 'lm', color = 'black')
    #> `geom_smooth()` using formula = 'y ~ x'
    #> Warning: Removed 2 rows containing non-finite outside the scale range
    #> (`stat_smooth()`).
    #> Warning: Removed 2 rows containing missing values or values outside the scale range
    #> (`geom_point()`).
    

    Created on 2024-08-30 with reprex v2.1.1