[R] Newbie: Drawing fitted lines on subset of data

Kevin Zembower kev|n @end|ng |rom zembower@org
Tue May 16 16:29:10 CEST 2023


Hello,

I's still working with my tsibble of weight data for the last 20 years. 
In addition to drawing an overall trend line, using lm, for the whole 
data set, I'd like to draw short lines that would recompute lm and draw 
it, say, just for the years from 2010:2015.

Here's a short example that I think illustrates what I'm trying to do. 
The commented out sections show what I've tried to far:

## Short example to test segments:

w <- tsibble(
     date = as.Date("2022-01-01") + 0:99,
     value = rnorm(100)
)

ggplot(data = w, mapping = aes(date, value)) +
     geom_smooth(method = "lm", se = FALSE) +
     geom_point()
     ## Below gives error about ignoring data
     ## geom_abline( data = w$date[25:75] )
     ## Gives error ''data' must be in <data.frame>'
     ## geom_smooth(data = w$date[25:35],
     ##             method = lm,
     ##             color = "black",
     ##             se = FALSE)

I'm thinking that this is probably easily done, but I'm struggling with 
how to subset the data in the middle of the pipeline.

Thanks for any advice and help.

-Kevin



More information about the R-help mailing list