2

I would like to extrapolate a linear fit to data over a subset of the data in ggplot2 beyond the region of that data (in order to visualize the danger of extrapolation). In the following code, I would like to expand the linear fit generated over the 1980-1990 frame into 1990-2000 such that I can then add a full fit for the entire time period and visualize the difference:

set.seed(123)           
frame <- data.frame(year = rep(1980:2000, 10), y = sample(1:1000, 210))
head(frame)

frame1 <- frame[frame$year %in% c(1980:1990),]
frame2 <- frame[frame$year %in% c(1980:2000),]

ggplot(frame1, aes(x = year, y = y)) + geom_point(shape = 1) + geom_smooth(method = lm) + xlim(1980, 2000)
ggplot(frame2, aes(x = year, y = y)) + geom_point(shape = 1) + geom_smooth(method = lm) + xlim(1980, 2000)

I'm new to ggplot2 so any thoughs on how to expand the linear fit from the first frame, then add the data and the new fit with a different color would be great. Thanks.

coding_heart
  • 1,245
  • 3
  • 25
  • 46

1 Answers1

7

You could try

ggplot(frame2, aes(x = year, y = y)) + 
  geom_point(shape = 1) + 
  geom_smooth(method = lm, se=FALSE) + 
  geom_smooth(data=frame1, method = lm, fullrange=TRUE, se=FALSE, colour="red") + 
  xlim(1980, 2000) 

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100