I have create the attached plot using ggplot and data I currently hold. I want to be able to add on a simple linear forecast for future years, ideally with some sort of confidence intervals but can't seem to find anyway to do it without calculating the forecasted values in a separate dataframe
CodePudding user response:
In addition to phivers and danloos solutions (if you only need a quick linear projection) you can extend the axis range and set the geom_smooth
layer to fill the plot, not only the data range:
data.frame(x = 1:10, y = 1:10 runif(10)) %>%
ggplot(aes(x,y))
geom_point()
geom_smooth(method = 'lm', ## lm for linear model
## extend smoother to plot range:
fullrange = TRUE
)
## extend axis beyond data range:
scale_x_continuous(limits = c(1,20))
sidenote: if you want to emphasise the trend in each plot facet, not the difference between facets, you can set the scales argument to facet_wrap
to any of 'free', 'free_x' or 'free_y': facet_wrap(..., scales = 'free')