How to add multiple trendlines to a scatter plot by groups

64 Views Asked by At

I'm trying to add multiple trendlines to a scatter plot in ggplot by age groups. I have 4 Age_range's 20_under, 21_24, 25_29, and 30_up so I'm looking to have 4 trendlines for each of these age groups

library(ggplot2)
library(ggpubr)

ggplot( cars, aes( x = horsepower, y = miles, color = Age_range))+ 
    geom_point()+
    geom_smooth(method=lm)+
    stat_cor(method = "pearson", label.x = 77, label.y = -3000)

I've hit a total roadblock. I tried working off a couple of similar questions on Stack Overflow but nothing seems to work.

2

There are 2 best solutions below

0
M-- On

You need to set the aes(color = ...) inside stat_cor as well or set inherit.aes = TRUE. Also, don't provide both label.x and label.y in stat_cor as that would cause the labels to print over each other when you have multiple groups.

library(ggplot2)
library(ggpubr)

## creating a sample dataset
set.seed(9)
MPG_Sample <- mpg[mpg$manufacturer %in% 
                    sample(unique(mpg$manufacturer), size = 4), ]

ggplot(MPG_Sample, aes(x = cty, y = displ, color = manufacturer))+ 
  geom_point()+
  geom_smooth(method=lm, formula = y ~ x) +
  stat_cor(method = "pearson", label.x = 25, aes(color = manufacturer))
# stat_cor(method = "pearson", label.x = 25, inherit.aes = TRUE)

Created on 2024-01-22 with reprex v2.0.2

0
Pedro J. Aphalo On

The plot can be also created using package 'ggpmisc'. This is an alternative answer.

library(ggplot2)
library(ggpmisc)

## creating a sample dataset
set.seed(9)
MPG_Sample <- mpg[mpg$manufacturer %in% 
                    sample(unique(mpg$manufacturer), size = 4), ]

# using ggpmisc instead of ggpubr
ggplot(MPG_Sample, aes(x = cty, y = displ, color = manufacturer)) + 
  geom_point()+
  stat_poly_line() +
  stat_correlation(use_label(c("R", "P")), label.x = "right")

Created on 2024-01-27 with reprex v2.1.0