Search code examples
rggplot2linear-regressionlm

Plot multiple regression lines on one plot in ggplot2


Sorry if this is a repeat question but I haven't managed to find an answer yet since my data frame has to be split. I am trying to plot two regression lines on one plot, with a regression line for data in period 1 (1815-1899)and a regression line for data in period 2 (1900-2013). I have used dplyr to split the data to run the two separate regressions but can't work out how to get them on the same graph as you seem to need the data frame in the ggplot() command for it to plot the line. Can anyone help?

Thanks.


library(tidyverse)

brest<-read.csv("brest.csv",header=TRUE) ## read in csv

brest<- na.omit(brest) ## get rid of NAs


brestp1<- select(filter(brest, period == 1),c(year,slr,period)) ## Divide into periods

brestp2<- select(filter(brest, period == 2),c(year,slr,period))

fit1 <- lm(slr ~ year, data = brestp1) ## Run lms
summary(fit1)

fit2<- lm(slr ~ year, data = brestp2)
summary(fit2)

## plot graph
ggplot(brestp1, aes(x = year, y = slr)) +  ### Need not only brestp1 but also brestp2
  geom_point() +
  stat_smooth(method = "lm",se=FALSE)+
  theme_classic()


## Data

## Brest period 1

structure(list(year = 1815:1820, slr = c(6926L, 6959L, 6945L, 
6965L, 6941L, 6909L), period = c(1L, 1L, 1L, 1L, 1L, 1L)), na.action = structure(c(`30` = 30L, 
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, 
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L, 
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L, 
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")

##Brest period 2

structure(list(year = 1900:1905, slr = c(6936L, 6916L, 6923L, 
6976L, 6931L, 6913L), period = c(2L, 2L, 2L, 2L, 2L, 2L)), na.action = structure(c(`30` = 30L, 
`31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, 
`37` = 37L, `38` = 38L, `39` = 39L, `51` = 51L, `52` = 52L, `53` = 53L, 
`54` = 54L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L, 
`142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L
), class = "omit"), row.names = c(NA, 6L), class = "data.frame")

Solution

  • Use geom_smooth with separate data:

    ggplot() +
      geom_smooth(aes(x = year, y = slr), data = brest1, 
                  method = "lm", se = FALSE, color = "red") + 
      geom_smooth(aes(x = year, y = slr), data = brest2, 
                  method = "lm", se = FALSE, color = "blue") + 
      geom_point(aes(x = year, y = slr), data = brest1, color = "red") + 
      geom_point(aes(x = year, y = slr), data = brest2, color = "blue")