geom_smooth with only a smidgen of smoothing?

Is there a way I can put in a formula for the loess method such that there is only a little bit of smoothing? The desired smoothed line would be a little less jagged with the beginning and ending more or less flat like the line plot in this case below. What would that formula be? I know the default loess uses formula = 'y ~ x'.

Thanks,
Jeff

library(tidyverse)
library("lubridate")
#> Loading required package: timechange
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library("reprex")

turtle_activity_gtm <-  read_csv("https://www.dropbox.com/s/unazvh79vcstsuu/mpt_act_rep_fcrawl.csv?dl=1")
#> Rows: 764 Columns: 64
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (22): beach, county, activity, ref_no, activity_comments, encountered?,...
#> dbl  (33): uid, activity_no, fcrawl, fcrawl_cum, nest_no, year, month, week,...
#> lgl   (6): final_treatment, light_management, relocation_reason, lost_nest, ...
#> date  (3): activity_date, emerge_date, inventory_date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.


ggplot(data = turtle_activity_gtm) +
  geom_line(aes(x=activity_date, y=fcrawl_cum),size=1.5) +
  geom_smooth(aes(x=activity_date, y=fcrawl_cum), method = 'loess', se = FALSE, size=0.5)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> `geom_smooth()` using formula = 'y ~ x'

Created on 2023-01-12 with reprex v2.0.2

You can adjust the span of the loess method. Here it is set to 0.1. The default is 0.75, I think.

library(tidyverse)
turtle_activity_gtm <-  read_csv("https://www.dropbox.com/s/unazvh79vcstsuu/mpt_act_rep_fcrawl.csv?dl=1")
#> Rows: 764 Columns: 64
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (22): beach, county, activity, ref_no, activity_comments, encountered?,...
#> dbl  (33): uid, activity_no, fcrawl, fcrawl_cum, nest_no, year, month, week,...
#> lgl   (6): final_treatment, light_management, relocation_reason, lost_nest, ...
#> date  (3): activity_date, emerge_date, inventory_date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

ggplot(data = turtle_activity_gtm) +
  geom_line(aes(x=activity_date, y=fcrawl_cum),size=1.5) +
  geom_smooth(aes(x=activity_date, y=fcrawl_cum), method = 'loess', 
              se = FALSE, size=0.5, span = 0.1)
#> `geom_smooth()` using formula 'y ~ x'

Created on 2023-01-12 with reprex v2.0.2

That does the trick. I played around with different spans. Looks like it can handle down to 0.05.

Thanks,
Jeff

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.