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.