Modifying plot from a general additive model created with a package

Hello. I am using a package wqtrends. It outputs trend plots based on a GAM output. I want to modfiy (add another series of data to these plots--using a second dataframe--it has the same X-values). But, I am getting an error. Any help would be appreciated. Thank you.

Some of the data to put into the model:

**head(tomod**)
# A tibble: 6 × 8
  date       station param value   doy cont_year    yr mo   
  <date>     <fct>   <fct> <dbl> <dbl>     <dbl> <dbl> <ord>
1 2014-12-01 9B      tp    0.24    335     2015.  2014 Dec  
2 2015-02-01 9B      tp    0.56     32     2015.  2015 Feb  
3 2015-07-01 9B      tp    0.122   182     2015.  2015 Jul  
4 2015-11-01 9B      tp    0.112   305     2016.  2015 Nov  
5 2016-02-01 9B      tp    0.163    32     2016.  2016 Feb  
6 2016-05-01 9B      tp    0.13    122     2016.  2016 May 
#> Error in parse(text = input): <text>:3:14: unexpected symbol
#> 2: # A tibble: 6 × 8
#> 3:   date       station

The data I want to add to the model output:
tibble::tribble(
~yr, ~threshold, ~top, ~thresh.comp,
2014, 0.2, 0.3, "gray",
2015, 0.2, 0.3, "gray",
2016, 0.2, 0.3, "white",
2017, 0.2, 0.3, "black",
2018, 0.2, 0.3, "gray",
2019, 0.2, 0.3, "white",
2020, 0.2, 0.3, "white",
2021, 0.2, 0.3, "white",
2022, 0.2, 0.3, "white",
2023, 0.2, 0.3, "white",
2024, 0.2, 0.3, "white"
)
The model: ```
mod <- anlz_gam(tomod, trans = "log10") ## Note: use trans = 'ident' to use untransformed data!
#> Error in anlz_gam(tomod, trans = "log10"): could not find function "anlz_gam"
mod
#> Error: object 'mod' not found

**The plot:**
``` r
plot3 <- show_trndseason(mod, doystr = 1, doyend = 365, justify = 'right', win = 5, ylab = ylab) 
#> Error in show_trndseason(mod, doystr = 1, doyend = 365, justify = "right", : could not find function "show_trndseason"

plot3 + geom_point(data = df2, aes(x = yr, y = top))
#> Error: object 'plot3' not found

Created on 2025-02-17 with reprex v2.1.1

1 Like

Right now it is producing the error because the function anlz_gam() can not be found this is because you have not loaded {wqtrends}. You can do this by using install.package("wqtrends") and then library(wqtrends).

Personally, I would opt to model your data using {mgcv} and assessing the model using {gratia} as I am guessing anlz_gam() is a wrapper around {mgcv} (checked and it indeed is a wrapper around {mgcv})and you will be better off/will understand your data and resulting model by using the full package and not a wrapper. I am, however, unfamiliar with {wqtrends} as a package so I maybe mistaken.

{gratia} is quite powerful in assisting with understanding and working with GAMMs produced by {mgcv}.