Many regression lines in a plot

How do you plot many regression lines in a plot? This concerns the textbook question from Forecasting: Principles and Practice. A dataset concerns winning times of Olympic running events. Categories include year, length (distance), sex (gender). The question askes to make a regression line for each plot to see the average rate of change for each event.

I could brute force this question but I want to find an efficient way to make multiple regression lines in a single plot. Is this possible?

Topic plotting rstudio time-series r

Category Data Science


I don't know the dataset but normally this would be done much more easily using the color dimension, something like this:

ggplot(aes(x=Year, y=Time, color=Length))
  +geom_point()
  +geom_smooth(method='lm')

Note that you may have to convert Length to a factor first, or use color=as.factor(Length) in aes.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.