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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue May 16 17:44:16 CEST 2023


Às 15:29 de 16/05/2023, Kevin Zembower via R-help escreveu:
> 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
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Try the following.
In the 2nd geom_smooth you need a subset of the data not of just one of 
its columns.



suppressPackageStartupMessages({
   library(tsibble)
   library(dplyr)
   library(ggplot2)
   library(lubridate)
})

ggplot(data = w, mapping = aes(date, value)) +
   geom_smooth(formula = y ~ x, method = "lm", se = FALSE) +
   geom_point() +
   geom_smooth(
     data = w %>% filter(year(date) >= 2010, year(date) <= 2015),
     mapping = aes(date, value),
     formula = y ~ x,
     method = lm,
     color = "black",
     se = FALSE
   )


Other ways to subset the data are


# dplyr
data = w %>% filter(year(date) %in% 2010:2015)
# base R
data = subset(w, year(date) %in% 2010:2015)


Hope this helps,

Rui Barradas



More information about the R-help mailing list