From a692fa51a27dba5ec8ea75b332dbdd4fea52a0af Mon Sep 17 00:00:00 2001 From: Weber Date: Fri, 25 Jul 2025 09:48:25 -0700 Subject: [PATCH 01/23] Nutrients vignette for eventual merge into main branch --- vignettes/Nutrients.qmd | 99 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 vignettes/Nutrients.qmd diff --git a/vignettes/Nutrients.qmd b/vignettes/Nutrients.qmd new file mode 100644 index 0000000..5265ae8 --- /dev/null +++ b/vignettes/Nutrients.qmd @@ -0,0 +1,99 @@ +--- +title: "NNI in StreamCat" +author: "Marc Weber" +date: "`r format(Sys.time(), '%d %B, %Y')`" +format: + html: + self-contained: true +--- + +## Farm N for Calapooia catchments + +```{r} +#|warning: false +#|message: false + +library(StreamCatTools) +start_comid = 23763517 +nldi_feature <- list(featureSource = "comid", featureID = start_comid) + +flowline_nldi <- nhdplusTools::navigate_nldi(nldi_feature, mode = "UT", data_source = "flowlines", distance=5000) + +# get StreamCat metrics +comids <- paste(as.integer(flowline_nldi$UT_flowlines$nhdplus_comid), collapse=",",sep="") + +df <- sc_get_data(metric='n_ff_2016', aoi='cat', comid=comids, showAreaSqKm=TRUE) + +flowline_nldi <- flowline_nldi$UT_flowlines +flowline_nldi$Farm_Nitrogon_2016 <- df$n_ff_2016cat[match(flowline_nldi$nhdplus_comid, df$comid)] + +basin <- nhdplusTools::get_nldi_basin(nldi_feature = nldi_feature) +``` + +## Map the Results +```{r} +#|warning: false +#|message: false +library(ggplot2) +library(ggspatial) +flowline_nldi |> + ggplot() + geom_sf(aes(colour = Farm_Nitrogon_2016)) + + scale_y_continuous() + + scale_color_distiller(palette = "Spectral") + + labs(color = "Kg Nitrogen") + + theme_minimal(12) + + ggtitle('Farm Nitrogen at the Catchment Scale for \nthe Calapooia River Basin 2016') +``` + +## Look at change through time Calapooia Farm Nitrogen +```{r} +#|warning: false +#|message: false +df1 <- sc_get_data(metric='n_ff_1987', aoi='cat', comid=comids) +df2 <- sc_get_data(metric='n_ff_2017', aoi='cat', comid=comids) +df2$n_ff_1987cat <- df1$n_ff_1987cat[match(df2$comid, df1$comid)] +df2$Farm_Nitrogen_Difference <- df2$n_ff_2017cat-df2$n_ff_1987cat +flowline_nldi$Farm_Nitrogen_Difference <- df2$Farm_Nitrogen_Difference[match(flowline_nldi$nhdplus_comid, df2$comid)] +``` + +## Map the Results +```{r} +#|warning: false +#|message: false +library(ggplot2) +library(ggspatial) +flowline_nldi |> + ggplot() + geom_sf(aes(colour = Farm_Nitrogen_Difference)) + + scale_y_continuous() + + labs(color = "Kg Nitrogen")+ + scale_color_distiller(palette = "Spectral") + + theme_minimal(12) + + ggtitle('Farm Nitrogen Change at the Catchment Scale for \nthe Calapooia River Basin from 1987 to 2017') +``` + +## Crop Fixation for the Calapooia Watershed +```{r} +#|warning: false +#|message: false + +options(scipen=3) +# get StreamCat metrics +df <- sc_get_data(metric='n_cf_2017', aoi='ws', comid=comids) + +flowline_nldi$CropNFixation <- df$n_cf_2017ws[match(flowline_nldi$nhdplus_comid, df$comid)] +``` + +## Map the Results +```{r} +#|warning: false +#|message: false +library(ggplot2) +library(ggspatial) +flowline_nldi |> + ggplot() + geom_sf(aes(colour = CropNFixation)) + + scale_y_continuous() + + scale_color_distiller(palette = "Spectral") + + labs(colour = "Kg Nitrogen") + + theme_minimal(12) + + ggtitle('Watershed Crop Nitrogen Fixation \nfor the Calapooia River Basin 2017') +``` \ No newline at end of file From 899d309e8c7c78ec315c08e3490f6ba7bef62745 Mon Sep 17 00:00:00 2001 From: Weber Date: Fri, 25 Jul 2025 10:39:15 -0700 Subject: [PATCH 02/23] Nutrients vignette in Rmd format rather than Qmd for vignette rendering --- vignettes/{Nutrients.qmd => NNI.Rmd} | 65 ++++++++++++++-------------- 1 file changed, 33 insertions(+), 32 deletions(-) rename vignettes/{Nutrients.qmd => NNI.Rmd} (65%) diff --git a/vignettes/Nutrients.qmd b/vignettes/NNI.Rmd similarity index 65% rename from vignettes/Nutrients.qmd rename to vignettes/NNI.Rmd index 5265ae8..095e897 100644 --- a/vignettes/Nutrients.qmd +++ b/vignettes/NNI.Rmd @@ -1,18 +1,34 @@ --- -title: "NNI in StreamCat" -author: "Marc Weber" -date: "`r format(Sys.time(), '%d %B, %Y')`" -format: - html: - self-contained: true +title: "National Nutrient Inventory (NNI) Data in StreamCat" +output: + html_document: + theme: flatly + keep_md: yes + number_sections: true + highlighted: default + toc: yes + toc_float: + collapsed: no + smooth_scroll: no + toc_depth: 2 +vignette: > + %\VignetteIndexEntry{Applications withs StreamCatTools} + %\VignetteEncoding{UTF-8}{inputenc} + %\VignetteEngine{knitr::rmarkdown} --- -## Farm N for Calapooia catchments - -```{r} -#|warning: false -#|message: false +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + warning = FALSE, + message = FALSE +) +``` +## Access Farm Nitrogn data for a watershed +In this example we access a single National Nutrient Inventory (NNI) metric for the Calapooia River basin using the `sc_get_data` function. We use the `nhdplusTools` library to pull in flowlines and the watershed boundary for the Calapooia River basin, plot the selected NNI metric for the Calapooia River and show the watershed. +```{r farmn, results='hide'} library(StreamCatTools) start_comid = 23763517 nldi_feature <- list(featureSource = "comid", featureID = start_comid) @@ -31,9 +47,7 @@ basin <- nhdplusTools::get_nldi_basin(nldi_feature = nldi_feature) ``` ## Map the Results -```{r} -#|warning: false -#|message: false +```{r map1, results='hide', message='false'} library(ggplot2) library(ggspatial) flowline_nldi |> @@ -46,9 +60,7 @@ flowline_nldi |> ``` ## Look at change through time Calapooia Farm Nitrogen -```{r} -#|warning: false -#|message: false +```{r change, results='hide', message='false'} df1 <- sc_get_data(metric='n_ff_1987', aoi='cat', comid=comids) df2 <- sc_get_data(metric='n_ff_2017', aoi='cat', comid=comids) df2$n_ff_1987cat <- df1$n_ff_1987cat[match(df2$comid, df1$comid)] @@ -57,11 +69,7 @@ flowline_nldi$Farm_Nitrogen_Difference <- df2$Farm_Nitrogen_Difference[match(flo ``` ## Map the Results -```{r} -#|warning: false -#|message: false -library(ggplot2) -library(ggspatial) +```{r map2, results='hide', message='false'} flowline_nldi |> ggplot() + geom_sf(aes(colour = Farm_Nitrogen_Difference)) + scale_y_continuous() + @@ -71,11 +79,8 @@ flowline_nldi |> ggtitle('Farm Nitrogen Change at the Catchment Scale for \nthe Calapooia River Basin from 1987 to 2017') ``` -## Crop Fixation for the Calapooia Watershed -```{r} -#|warning: false -#|message: false - +## Crop Fixation for the Calapooia Watershed +```{r crop, results='hide', message='false'} options(scipen=3) # get StreamCat metrics df <- sc_get_data(metric='n_cf_2017', aoi='ws', comid=comids) @@ -84,11 +89,7 @@ flowline_nldi$CropNFixation <- df$n_cf_2017ws[match(flowline_nldi$nhdplus_comid, ``` ## Map the Results -```{r} -#|warning: false -#|message: false -library(ggplot2) -library(ggspatial) +```{r map3, results='hide', message='false'} flowline_nldi |> ggplot() + geom_sf(aes(colour = CropNFixation)) + scale_y_continuous() + From a2dcce1813b6bec5fb8a46d925ab91476f528d60 Mon Sep 17 00:00:00 2001 From: Weber Date: Fri, 5 Sep 2025 11:29:15 -0700 Subject: [PATCH 03/23] typo in vignette --- vignettes/NNI.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/NNI.Rmd b/vignettes/NNI.Rmd index 095e897..a87ca05 100644 --- a/vignettes/NNI.Rmd +++ b/vignettes/NNI.Rmd @@ -26,7 +26,7 @@ knitr::opts_chunk$set( ) ``` -## Access Farm Nitrogn data for a watershed +## Access Farm Nitrogen data for a watershed In this example we access a single National Nutrient Inventory (NNI) metric for the Calapooia River basin using the `sc_get_data` function. We use the `nhdplusTools` library to pull in flowlines and the watershed boundary for the Calapooia River basin, plot the selected NNI metric for the Calapooia River and show the watershed. ```{r farmn, results='hide'} library(StreamCatTools) From c757222e1fd66b1ca743bb59b0abc976678e41ba Mon Sep 17 00:00:00 2001 From: Weber Date: Wed, 10 Sep 2025 11:02:51 -0700 Subject: [PATCH 04/23] Increment version number to 0.6.0.9000 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7e4d1ab..025b3ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: StreamCatTools Type: Package Title: 'StreamCatTools' -Version: 0.6.0 +Version: 0.6.0.9000 Authors@R: c(person(given = "Marc", family = "Weber", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 761234b..b393d06 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# StreamCatTools (development version) + # StreamCatTools 0.6.0 - Added functionality in `sc_get_data` and `lc_get_data` to accommodate large numbers (e.g. > 700) COMIDs in header for POST request From 5f334d3b2fc9842b130d4c3c908b86d09a694409 Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:38:43 -0700 Subject: [PATCH 05/23] added lc_get_nni function --- R/lc_get_data.R | 156 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/R/lc_get_data.R b/R/lc_get_data.R index 4bd84a7..499a5b6 100644 --- a/R/lc_get_data.R +++ b/R/lc_get_data.R @@ -257,3 +257,159 @@ lc_nlcd <- function(year = '2019', aoi = NULL, comid = NULL, # End of function. Return a data frame. return(final_df) } + + +#' @title Get NNI +#' +#' @description +#' Function to get all NNI data available for a given year. +#' +#' @author +#' Selia Markley +#' +#' @param year Years(s) of NNI metrics to query. +#' Only valid NNI years are accepted (1987:2017) +#' Syntax: year=, +#' +#' @param aoi Specify the area of interest described by a metric. By default, all available areas of interest +#' for a given metric are returned. +#' Syntax: areaOfInterest=, +#' Values: catchment|watershed +#' +#' @param comid Return metric information for specific COMIDs +#' Syntax: comid=, +#' +#' @param showAreaSqKm Return the area in square kilometers of a given area of interest. +#' The default value is true. +#' Values: true|false +#' +#' @param showPctFull Return the pctfull for each dataset. The default value is false. +#' Values: true|false +#' +#' @param countOnly Return a CSV containing only the row count (ROWCOUNT) and the column +#' count (COLUMNCOUNT) that the server expects to return in a request. The default value is false. +#' Values: true|false +#' +#' @return A tibble of desired StreamCat metrics +#' @export +#' +#' @examples\donttest{ +#' df <- lc_getnni(year='1987, 1990, 2005, 2017', aoi='cat,ws', +#' comid='23783629,23794487,23812618') +#' +#' df <- lc_getnni(year='2015', aoi='cat', +#' comid='23783629', countOnly=TRUE) +#' +#' df <- lc_getnni(comid='23783629', year='2011, 2012', aoi='ws') +#' } + +lc_get_nni <- function(year, aoi = NULL, comid = NULL, + showAreaSqKm = TRUE, showPctFull = NULL, + countOnly = NULL) { + # year must be a character string. + year_chr <- as.character(year) + # split multiple years supplied as a single string into + # a vector of years. + year_vec <- unlist(strsplit(x = year_chr, + split = ",|, ")) + # Vector of valid NNI years to check inputs against. + valid_years <- c('1987', + '1988', + '1989', + '1990', + '1991', + '1992', + '1993', + '1994', + '1995', + '1996', + '1997', + '1998', + '1999', + '2000', + '2001', + '2002', + '2003', + '2004', + '2005', + '2006', + '2007', + '2008', + '2009', + '2010', + '2011', + '2012', + '2013', + '2014', + '2015', + '2016', + '2017') + # Stop early if any of the year(s) supplied are not found in the valid + # years vec. + stopifnot( + "year must be a valid NNI year" = any(year_vec %in% valid_years) + ) + # Vector of NNI metric names. + nni <- c( + 'n_leg_', + 'n_ags_', + 'n_ff_', + 'n_uf_', + 'n_cf_', + 'n_cr_', + 'n_hw_', + 'n_lw_', + 'p_leg_', + 'p_ags_', + 'p_ff_', + 'p_uf_', + 'p_cr_', + 'p_hw_', + 'p_lw_' + ) + # Add n_dep for available years + ndep_year_vec <- year_vec[!year_vec %in% c('1987', '1988', '1989')] + ndep_comb <- expand.grid('n_dep_', ndep_year_vec) + ndep_mets <- paste0(ndep_comb$Var1, + ndep_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Add p_dep for available years + pdep_year_vec <- year_vec[!year_vec %in% c('1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', + '2014', '2015', '2016', '2017')] + pdep_comb <- expand.grid('p_dep_', pdep_year_vec) + pdep_mets <- paste0(pdep_comb$Var1, + pdep_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Add n_usgsww and p_usgsww for available years + ww_year_vec <- year_vec[year_vec %in% c('1988', '1990', '1992', '1996', '2000', '2004', '2008', '2012')] + ww_comb <- expand.grid(c('p_usgsww_', 'n_usgsww_'), ww_year_vec) + ww_mets <- paste0(ww_comb$Var1, + ww_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Create a data frame of all NNI Metric and year combinations. + all_comb <- expand.grid(nni, year_vec) + # Concatenate the NLCD metric name with the supplied year(s) to create + # valid metric names to submit to the API. + nni_mets <- paste0(all_comb$Var1, + all_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Combine all NNI metrics + nni_mets_all <- paste0(nni_mets, ",", ndep_mets, ",", pdep_mets, ",", ww_mets) + + # Query the API. + final_df <- lc_get_data( + metric = nni_mets_all, + aoi = aoi, + comid = comid, + showAreaSqKm = showAreaSqKm, + showPctFull = showPctFull, + countOnly = countOnly + ) + # End of function. Return a data frame. + return(final_df) +} + From be22c7ab35d6aeb5e6f94801fbfcef7b23bd2c32 Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:39:03 -0700 Subject: [PATCH 06/23] added sc_get_nni function --- R/sc_get_data.R | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/R/sc_get_data.R b/R/sc_get_data.R index dfb0495..92575cc 100644 --- a/R/sc_get_data.R +++ b/R/sc_get_data.R @@ -304,3 +304,157 @@ sc_nlcd <- function(year = '2019', ignore_unused_imports <- function() { curl::curl_parse_url() } + +#' @title Get NNI +#' +#' @description +#' Function to get all NNI data available for a given year. +#' +#' @author +#' Selia Markley +#' +#' @param year Years(s) of NNI metrics to query. +#' Only valid NNI years are accepted (1987:2017) +#' Syntax: year=, +#' +#' @param aoi Specify the area of interest described by a metric. By default, all available areas of interest +#' for a given metric are returned. +#' Syntax: areaOfInterest=, +#' Values: catchment|watershed +#' +#' @param comid Return metric information for specific COMIDs +#' Syntax: comid=, +#' +#' @param showAreaSqKm Return the area in square kilometers of a given area of interest. +#' The default value is true. +#' Values: true|false +#' +#' @param showPctFull Return the pctfull for each dataset. The default value is false. +#' Values: true|false +#' +#' @param countOnly Return a CSV containing only the row count (ROWCOUNT) and the column +#' count (COLUMNCOUNT) that the server expects to return in a request. The default value is false. +#' Values: true|false +#' +#' @return A tibble of desired StreamCat metrics +#' @export +#' +#' @examples\donttest{ +#' df <- sc_getnni(year='1987, 1990, 2005, 2017', aoi='cat,ws', +#' comid='23783629,23794487,23812618') +#' +#' df <- sc_getnni(year='2015', aoi='cat', +#' comid='23783629', countOnly=TRUE) +#' +#' df <- sc_getnni(comid='23783629', year='2011, 2012', aoi='ws') +#' } + +sc_get_nni <- function(year, aoi = NULL, comid = NULL, + showAreaSqKm = TRUE, showPctFull = NULL, + countOnly = NULL) { + # year must be a character string. + year_chr <- as.character(year) + # split multiple years supplied as a single string into + # a vector of years. + year_vec <- unlist(strsplit(x = year_chr, + split = ",|, ")) + # Vector of valid NNI years to check inputs against. + valid_years <- c('1987', + '1988', + '1989', + '1990', + '1991', + '1992', + '1993', + '1994', + '1995', + '1996', + '1997', + '1998', + '1999', + '2000', + '2001', + '2002', + '2003', + '2004', + '2005', + '2006', + '2007', + '2008', + '2009', + '2010', + '2011', + '2012', + '2013', + '2014', + '2015', + '2016', + '2017') + # Stop early if any of the year(s) supplied are not found in the valid + # years vec. + stopifnot( + "year must be a valid NNI year" = any(year_vec %in% valid_years) + ) + # Vector of NNI metric names. + nni <- c( + 'n_leg_', + 'n_ags_', + 'n_ff_', + 'n_uf_', + 'n_cf_', + 'n_cr_', + 'n_hw_', + 'n_lw_', + 'p_leg_', + 'p_ags_', + 'p_ff_', + 'p_uf_', + 'p_cr_', + 'p_hw_', + 'p_lw_' + ) + # Add n_dep for available years + ndep_year_vec <- year_vec[!year_vec %in% c('1987', '1988', '1989')] + ndep_comb <- expand.grid('n_dep_', ndep_year_vec) + ndep_mets <- paste0(ndep_comb$Var1, + ndep_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Add p_dep for available years + pdep_year_vec <- year_vec[!year_vec %in% c('1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', + '2014', '2015', '2016', '2017')] + pdep_comb <- expand.grid('p_dep_', pdep_year_vec) + pdep_mets <- paste0(pdep_comb$Var1, + pdep_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Add n_usgsww and p_usgsww for available years + ww_year_vec <- year_vec[year_vec %in% c('1988', '1990', '1992', '1996', '2000', '2004', '2008', '2012')] + ww_comb <- expand.grid(c('p_usgsww_', 'n_usgsww_'), ww_year_vec) + ww_mets <- paste0(ww_comb$Var1, + ww_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Create a data frame of all NNI Metric and year combinations. + all_comb <- expand.grid(nni, year_vec) + # Concatenate the NLCD metric name with the supplied year(s) to create + # valid metric names to submit to the API. + nni_mets <- paste0(all_comb$Var1, + all_comb$Var2, + collapse = ",", + recycle0 = TRUE) + # Combine all NNI metrics + nni_mets_all <- paste0(nni_mets, ",", ndep_mets, ",", pdep_mets, ",", ww_mets) + + # Query the API. + final_df <- sc_get_data( + metric = nni_mets_all, + aoi = aoi, + comid = comid, + showAreaSqKm = showAreaSqKm, + showPctFull = showPctFull, + countOnly = countOnly + ) + # End of function. Return a data frame. + return(final_df) +} From ebac73ecea3aaec76b7e48e7d160df44611b417b Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:39:39 -0700 Subject: [PATCH 07/23] created lc_plot file and added lc_plot_nni function --- R/lc_plot.R | 325 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 325 insertions(+) create mode 100644 R/lc_plot.R diff --git a/R/lc_plot.R b/R/lc_plot.R new file mode 100644 index 0000000..b44cc08 --- /dev/null +++ b/R/lc_plot.R @@ -0,0 +1,325 @@ +#' Plot National Nutrient Inventory data for lakes +#' +#' @description +#' Function to plot time series of nitrogen and phosphorus budgets for a given lake +#' COMID. This function allows a user to return a time series of major inputs, +#' outputs, and derived metrics of nitrogen and phosphorus. Plot is returned as an +#' object +#' +#' @author +#' Selia Markley +#' +#' @param comid Identifier of lake COMID user wants to plot NNI data for. Must be a character string +#' with the COMID digit. +#' Syntax: com= +#' +#' @param include.nue Include time series of nitrogen use efficiency in the returned plot. +#' The default value is false. +#' Values: true|false +#' +#' @return +#' Return plot as an object. +#' @export +#' +#' @examples +#' \donttest +#' p <- lc_plotnni(comid='23794487') +#' p <- lc_plotnni(comid='23794487', include.nue=TRUE) + +lc_plotnni <- function(comid, include.nue = FALSE){ + + # Get StreamCat data + nni <- lc_get_data(metric = 'n_dep_1990,n_ff_1990,n_uf_1990,n_lw_1990,n_hw_1990,n_ags_1990,n_cf_1990,n_cr_1990,p_cr_1990,p_lw_1990,p_hw_1990,p_uf_1990,p_ff_1990,p_ags_1990,n_dep_1991,n_ff_1991,n_uf_1991,n_lw_1991,n_hw_1991,n_ags_1991,n_cf_1991,n_cr_1991,p_cr_1991,p_lw_1991,p_hw_1991,p_uf_1991,p_ff_1991,p_ags_1991,n_dep_1992,n_ff_1992,n_uf_1992,n_lw_1992,n_hw_1992,n_ags_1992,n_cf_1992,n_cr_1992,p_cr_1992,p_lw_1992,p_hw_1992,p_uf_1992,p_ff_1992,p_ags_1992,n_dep_1993,n_ff_1993,n_uf_1993,n_lw_1993,n_hw_1993,n_ags_1993,n_cf_1993,n_cr_1993,p_cr_1993,p_lw_1993,p_hw_1993,p_uf_1993,p_ff_1993,p_ags_1993,n_dep_1994,n_ff_1994,n_uf_1994,n_lw_1994,n_hw_1994,n_ags_1994,n_cf_1994,n_cr_1994,p_cr_1994,p_lw_1994,p_hw_1994,p_uf_1994,p_ff_1994,p_ags_1994,n_dep_1995,n_ff_1995,n_uf_1995,n_lw_1995,n_hw_1995,n_ags_1995,n_cf_1995,n_cr_1995,p_cr_1995,p_lw_1995,p_hw_1995,p_uf_1995,p_ff_1995,p_ags_1995,n_dep_1996,n_ff_1996,n_uf_1996,n_lw_1996,n_hw_1996,n_ags_1996,n_cf_1996,n_cr_1996,p_cr_1996,p_lw_1996,p_hw_1996,p_uf_1996,p_ff_1996,p_ags_1996,n_dep_1997,n_ff_1997,n_uf_1997,n_lw_1997,n_hw_1997,n_ags_1997,n_cf_1997,n_cr_1997,p_cr_1997,p_lw_1997,p_hw_1997,p_uf_1997,p_ff_1997,p_ags_1997,n_dep_1998,n_ff_1998,n_uf_1998,n_lw_1998,n_hw_1998,n_ags_1998,n_cf_1998,n_cr_1998,p_cr_1998,p_lw_1998,p_hw_1998,p_uf_1998,p_ff_1998,p_ags_1998,n_dep_1999,n_ff_1999,n_uf_1999,n_lw_1999,n_hw_1999,n_ags_1999,n_cf_1999,n_cr_1999,p_cr_1999,p_lw_1999,p_hw_1999,p_uf_1999,p_ff_1999,p_ags_1999,n_dep_2000,n_ff_2000,n_uf_2000,n_lw_2000,n_hw_2000,n_ags_2000,n_cf_2000,n_cr_2000,p_cr_2000,p_lw_2000,p_hw_2000,p_uf_2000,p_ff_2000,p_ags_2000,n_dep_2001,n_ff_2001,n_uf_2001,n_lw_2001,n_hw_2001,n_ags_2001,n_cf_2001,n_cr_2001,p_cr_2001,p_lw_2001,p_hw_2001,p_uf_2001,p_ff_2001,p_ags_2001,n_dep_2002,n_ff_2002,n_uf_2002,n_lw_2002,n_hw_2002,n_ags_2002,n_cf_2002,n_cr_2002,p_cr_2002,p_lw_2002,p_hw_2002,p_uf_2002,p_ff_2002,p_ags_2002,n_dep_2003,n_ff_2003,n_uf_2003,n_lw_2003,n_hw_2003,n_ags_2003,n_cf_2003,n_cr_2003,p_cr_2003,p_lw_2003,p_hw_2003,p_uf_2003,p_ff_2003,p_ags_2003,n_dep_2004,n_ff_2004,n_uf_2004,n_lw_2004,n_hw_2004,n_ags_2004,n_cf_2004,n_cr_2004,p_cr_2004,p_lw_2004,p_hw_2004,p_uf_2004,p_ff_2004,p_ags_2004,n_dep_2005,n_ff_2005,n_uf_2005,n_lw_2005,n_hw_2005,n_ags_2005,n_cf_2005,n_cr_2005,p_cr_2005,p_lw_2005,p_hw_2005,p_uf_2005,p_ff_2005,p_ags_2005,n_dep_2006,n_ff_2006,n_uf_2006,n_lw_2006,n_hw_2006,n_ags_2006,n_cf_2006,n_cr_2006,p_cr_2006,p_lw_2006,p_hw_2006,p_uf_2006,p_ff_2006,p_ags_2006,n_dep_2007,n_ff_2007,n_uf_2007,n_lw_2007,n_hw_2007,n_ags_2007,n_cf_2007,n_cr_2007,p_cr_2007,p_lw_2007,p_hw_2007,p_uf_2007,p_ff_2007,p_ags_2007,n_dep_2008,n_ff_2008,n_uf_2008,n_lw_2008,n_hw_2008,n_ags_2008,n_cf_2008,n_cr_2008,p_cr_2008,p_lw_2008,p_hw_2008,p_uf_2008,p_ff_2008,p_ags_2008,n_dep_2009,n_ff_2009,n_uf_2009,n_lw_2009,n_hw_2009,n_ags_2009,n_cf_2009,n_cr_2009,p_cr_2009,p_lw_2009,p_hw_2009,p_uf_2009,p_ff_2009,p_ags_2009,n_dep_2010,n_ff_2010,n_uf_2010,n_lw_2010,n_hw_2010,n_ags_2010,n_cf_2010,n_cr_2010,p_cr_2010,p_lw_2010,p_hw_2010,p_uf_2010,p_ff_2010,p_ags_2010,n_dep_2011,n_ff_2011,n_uf_2011,n_lw_2011,n_hw_2011,n_ags_2011,n_cf_2011,n_cr_2011,p_cr_2011,p_lw_2011,p_hw_2011,p_uf_2011,p_ff_2011,p_ags_2011,n_dep_2012,n_ff_2012,n_uf_2012,n_lw_2012,n_hw_2012,n_ags_2012,n_cf_2012,n_cr_2012,p_cr_2012,p_lw_2012,p_hw_2012,p_uf_2012,p_ff_2012,p_ags_2012,n_dep_2013,n_ff_2013,n_uf_2013,n_lw_2013,n_hw_2013,n_ags_2013,n_cf_2013,n_cr_2013,p_cr_2013,p_lw_2013,p_hw_2013,p_uf_2013,p_ff_2013,p_ags_2013,n_dep_2014,n_ff_2014,n_uf_2014,n_lw_2014,n_hw_2014,n_ags_2014,n_cf_2014,n_cr_2014,p_cr_2014,p_lw_2014,p_hw_2014,p_uf_2014,p_ff_2014,p_ags_2014,n_dep_2015,n_ff_2015,n_uf_2015,n_lw_2015,n_hw_2015,n_ags_2015,n_cf_2015,n_cr_2015,p_cr_2015,p_lw_2015,p_hw_2015,p_uf_2015,p_ff_2015,p_ags_2015,n_dep_2016,n_ff_2016,n_uf_2016,n_lw_2016,n_hw_2016,n_ags_2016,n_cf_2016,n_cr_2016,p_cr_2016,p_lw_2016,p_hw_2016,p_uf_2016,p_ff_2016,p_ags_2016,n_dep_2017,n_ff_2017,n_uf_2017,n_lw_2017,n_hw_2017,n_ags_2017,n_cf_2017,n_cr_2017,p_cr_2017,p_lw_2017,p_hw_2017,p_uf_2017,p_ff_2017,p_ags_2017', + aoi='ws', + comid = comid, + showAreaSqKm = FALSE, + showPctFull = FALSE) + + #Create N inputs df + + nin <- nni[, grepl("^(n)", names(nni)) & !grepl("(cr)", names(nni)) & !grepl("(ags)", names(nni))] + + names(nin) <- sapply(names(nin), function(col){ + substr(col, 3, nchar(col) -2) + }) + + nin <- nin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) + + #Create P inputs df + + pin <- nni[, grepl("^(p)", names(nni)) & !grepl("(cr)", names(nni)) & !grepl("(ags)", names(nni))] + + names(pin) <- sapply(names(pin), function(col){ + substr(col, 3, nchar(col) -2) + }) + + pin <- pin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) + + #Create N dfs for lines (cr, agsur, nue) + + nlines <- nni[, grepl("^n_cr|^n_ags", names(nni))] + + names(nlines) <- sapply(names(nlines), function(col){ + substr(col, 3, nchar(col) -2) + }) + + nlines <- nlines %>% + pivot_longer( + cols = everything(), + names_to = c("metric","year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) %>% + pivot_wider( + names_from = 'metric', + values_from = 'value' + ) %>% + mutate(totag = ags + cr) %>% + mutate(nue = (cr / totag) * 100) %>% + pivot_longer( + cols = !year, + names_to="metric", + values_to="value") + + ncrag <- nlines %>% + filter(metric %in% c('ags', 'cr')) + + nue <- nlines %>% + filter(metric == 'nue') %>% + pivot_wider(names_from = 'metric', + values_from = 'value') + + #Create P dfs for lines (cr, agsur, pue) + + plines <- nni[, grepl("^p_cr|^p_ags", names(nni))] + + names(plines) <- sapply(names(plines), function(col){ + substr(col, 3, nchar(col) -2) + }) + + + plines <- plines %>% + pivot_longer( + cols = everything(), + names_to = c("metric","year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) %>% + pivot_wider( + names_from = 'metric', + values_from = 'value' + ) %>% + mutate(totag = ags + cr) %>% + mutate(nue = (cr / totag) * 100) %>% + pivot_longer( + cols = !year, + names_to="metric", + values_to="value") + + pcrag <- plines %>% + filter(metric %in% c('ags', 'cr')) + + pue <- plines %>% + filter(metric == 'nue') %>% + pivot_wider(names_from = 'metric', + values_from = 'value') + + pdf <- bind_rows(plines, pin) + + ndf <- bind_rows(nlines, nin) + + #create estimate column + knownfertyrs <- c(1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004, + 2005,2006,2007,2008,2009,2010,2011,2012,2017) + nwsin <- nin %>% + mutate(estimated=case_when( + metric == "dep" ~ FALSE, + metric == "hw" ~ FALSE, + metric == "cf" & year %in% c(1987,1992,1997,2002,2007,2012, 2017) ~ FALSE, + metric == "ff" & year %in% knownfertyrs ~ FALSE, + metric == "uf" & year %in% knownfertyrs ~ FALSE, + metric == "lw" & year %in% c(1987,1992,1997,2002,2007,2012,2017) ~ FALSE, + TRUE ~ TRUE + )) + + pwsin <- pin %>% + filter(metric != 'cr') %>% + mutate(estimated=case_when( + metric == "hw" ~ FALSE, + metric == "lw" & year %in% c(1987,1992,1997,2002,2007,2012,2017) ~ FALSE, + metric == "ff" & year %in% knownfertyrs ~ FALSE, + metric == "uf" & year %in% knownfertyrs ~ FALSE, + TRUE ~ TRUE + )) + + #get ready for plot + colorsn <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black', 'dep'='#6db6ff', 'cf'='#FFD700') + colorsp <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black') + + nwsin$metric <- factor(nwsin$metric, levels = c('uf','hw','dep','lw','cf','ff')) + pwsin$metric <- factor(pwsin$metric, levels = c('uf','hw','lw','ff')) + + + #create titles with higher level include.nue param + if (include.nue == TRUE){ + nbartitle <- 'b)' + pbartitle <- 'd)' + nuetitle <- 'a)' + puetitle <- 'c)' + } else{ + nbartitle <- 'a)' + pbartitle <- 'b)' + nuetitle <- ' ' + puetitle <- ' ' + } + + #create N bar plot + nbar <- ggplot() + + geom_bar_pattern(data = nwsin, + aes(x=year,y=value, fill=metric, + pattern=factor(estimated, levels=c(TRUE,FALSE), + labels=c('Estimated','Non-Estimated'))), + pattern = ifelse(nwsin$estimated, 'stripe','none'), + pattern_color='white', + pattern_density=0.05, + pattern_fill = 'white', + pattern_alpha = 0.5, + pattern_spacing=0.025, + stat='identity', position='stack', + pattern_size=0.05) + + labs(title = nbartitle, + y = "Inputs, Crop Removal, Surplus (million kg)", + x = " ") + + scale_fill_manual(values=colorsn, + labels = c('ff' = 'Farm Fertilizer', + 'uf' = 'Urban Fertilizer', + 'cf' = 'Crop N-Fixation', + 'lw' = 'Livestock Manure', + 'hw' = 'Human Waste', + 'dep' = 'Total Deposition')) + + scale_pattern_manual(name='Estimate Status', + values=c('Estimated'='stripe','Non-estimated'='none')) + + geom_line(data=ncrag, + aes(x=year,y=value, linetype=metric), + linewidth=1.25, color="black") + + scale_linetype_manual(values = + c("ags"="solid", "cr"="dotted"), + labels = c('ags' = 'Agricultural Surplus', + 'cr' = 'Crop Removal')) + + guides(fill= + guide_legend(order=1, override.aes = list(pattern='none')), + pattern= + guide_legend(order=2, override.aes = list(fill='grey')), + linetype= + guide_legend(title=NULL)) + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + scale_color_manual(values=c("Agricultural Surplus"="black"), + guide = 'none') + + theme_bw() + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.y = element_text(size=9), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + #create p bar plot + pbar <- ggplot() + + geom_bar_pattern(data = pwsin, + aes(x=year,y=value, fill=metric, + pattern=factor(estimated, levels=c(TRUE,FALSE), + labels=c('Estimated','Non-Estimated'))), + pattern = ifelse(pwsin$estimated, 'stripe','none'), + pattern_color='white', + pattern_density=0.05, + pattern_fill = 'white', + pattern_alpha = 0.5, + pattern_spacing=0.025, + stat='identity', position='stack', pattern_size=0.05) + + labs(title = pbartitle, + y = "Inputs, Crop Removal, Surplus (million kg)", + x = " ") + + scale_fill_manual(values=colorsp) + + scale_pattern_manual(name='Estimate Status', + values=c('Estimated'='stripe', + 'Non-estimated'='none')) + + geom_line(data=pcrag, + aes(x=year,y=value, + linetype=metric), + linewidth=1.25, color="black") + + scale_linetype_manual(values = + c("ags"="solid", "cr"="dotted")) + + guides(fill= + guide_legend(order=1, override.aes = list(pattern='none')), + pattern= + guide_legend(order=2, override.aes = list(fill='grey')), + linetype= + guide_legend(title=NULL)) + + guides(fill="none", pattern = "none", linetype="none") + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + scale_color_manual(values=c("Agricultural Surplus"="black"), + guide = 'none') + + theme_bw() + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.y = element_text(size=9), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + #create nue line plots + nue <- ggplot() + + geom_line(data=nue, aes(x=year,y=nue), linewidth=1.25, color='seagreen')+ + theme_bw() + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + labs(title = nuetitle, + y = "%", + x=" ") + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.x = element_text(size=9), + axis.title.y = element_text(size=9)) + + pue <- ggplot() + + geom_line(data=pue, aes(x=year,y=nue, lty='Nutrient Use Efficiency'), linewidth=1.25, color="seagreen") + + theme_bw() + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + labs(title = puetitle, + y = "%", + x="Year") + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.x = element_text(size=9), + axis.title.y = element_text(size=9, hjust=0.5), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + guides(fill="none", pattern = "none", linetype="none") + + #export final figure + inputs <- (nbar / pbar) + plot_layout(guides = "collect") + nue <- (nue / pue) + plot_layout(guides = "collect") + + if (include.nue == TRUE){ + timenni <- (nue | inputs) + } + else { + timenni <- inputs + } + + return(timenni) + +} From 26194a23a3fedb24fecb5bb465610d316cf44472 Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:40:15 -0700 Subject: [PATCH 08/23] created sc_plot file and added sc_plot_nni function --- R/sc_plot.R | 352 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 352 insertions(+) create mode 100644 R/sc_plot.R diff --git a/R/sc_plot.R b/R/sc_plot.R new file mode 100644 index 0000000..a82bc5f --- /dev/null +++ b/R/sc_plot.R @@ -0,0 +1,352 @@ +#' @title Plot National Nutrient Inventory data for streams +#' +#' @description +#' Function to plot time series of nitrogen and phosphorus budgets for a given stream +#' COMID. This function allows a user to return a time series of major inputs, +#' outputs, and derived metrics of nitrogen and phosphorus. Plot is returned as an +#' object +#' +#' @author +#' Selia Markley +#' +#' @param comid Identifier of stream COMID user wants to plot NNI data for. Must be a character string +#' with the COMID digit. +#' Syntax: com= +#' +#' @param include.nue Include time series of nitrogen use efficiency in the returned plot. +#' The default value is false. +#' Values: true|false +#' +#' @return +#' Return plot as an object. +#' @export +#' +#' @examples +#' \donttest +#' p <- sc_plotnni(comid='1337420') +#' p <- sc_plotnni(comid='1337420', include.nue=TRUE) + +sc_plotnni <- function(com, include.nue = FALSE){ + + # Get StreamCat data + nni <- sc_get_data(metric = 'n_dep_1990,n_ff_1990,n_uf_1990,n_lw_1990,n_hw_1990,n_ags_1990,n_cf_1990,n_cr_1990,p_cr_1990,p_lw_1990,p_hw_1990,p_uf_1990,p_ff_1990, + p_ags_1990,n_dep_1991,n_ff_1991,n_uf_1991,n_lw_1991,n_hw_1991,n_ags_1991,n_cf_1991,n_cr_1991,p_cr_1991,p_lw_1991,p_hw_1991,p_uf_1991,p_ff_1991,p_ags_1991, + n_dep_1992,n_ff_1992,n_uf_1992,n_lw_1992,n_hw_1992,n_ags_1992,n_cf_1992,n_cr_1992,p_cr_1992,p_lw_1992,p_hw_1992,p_uf_1992,p_ff_1992,p_ags_1992, + n_dep_1993,n_ff_1993,n_uf_1993,n_lw_1993,n_hw_1993,n_ags_1993,n_cf_1993,n_cr_1993,p_cr_1993,p_lw_1993,p_hw_1993,p_uf_1993,p_ff_1993,p_ags_1993, + n_dep_1994,n_ff_1994,n_uf_1994,n_lw_1994,n_hw_1994,n_ags_1994,n_cf_1994,n_cr_1994,p_cr_1994,p_lw_1994,p_hw_1994,p_uf_1994,p_ff_1994,p_ags_1994, + n_dep_1995,n_ff_1995,n_uf_1995,n_lw_1995,n_hw_1995,n_ags_1995,n_cf_1995,n_cr_1995,p_cr_1995,p_lw_1995,p_hw_1995,p_uf_1995,p_ff_1995,p_ags_1995, + n_dep_1996,n_ff_1996,n_uf_1996,n_lw_1996,n_hw_1996,n_ags_1996,n_cf_1996,n_cr_1996,p_cr_1996,p_lw_1996,p_hw_1996,p_uf_1996,p_ff_1996,p_ags_1996, + n_dep_1997,n_ff_1997,n_uf_1997,n_lw_1997,n_hw_1997,n_ags_1997,n_cf_1997,n_cr_1997,p_cr_1997,p_lw_1997,p_hw_1997,p_uf_1997,p_ff_1997,p_ags_1997, + n_dep_1998,n_ff_1998,n_uf_1998,n_lw_1998,n_hw_1998,n_ags_1998,n_cf_1998,n_cr_1998,p_cr_1998,p_lw_1998,p_hw_1998,p_uf_1998,p_ff_1998,p_ags_1998, + n_dep_1999,n_ff_1999,n_uf_1999,n_lw_1999,n_hw_1999,n_ags_1999,n_cf_1999,n_cr_1999,p_cr_1999,p_lw_1999,p_hw_1999,p_uf_1999,p_ff_1999,p_ags_1999, + n_dep_2000,n_ff_2000,n_uf_2000,n_lw_2000,n_hw_2000,n_ags_2000,n_cf_2000,n_cr_2000,p_cr_2000,p_lw_2000,p_hw_2000,p_uf_2000,p_ff_2000,p_ags_2000, + n_dep_2001,n_ff_2001,n_uf_2001,n_lw_2001,n_hw_2001,n_ags_2001,n_cf_2001,n_cr_2001,p_cr_2001,p_lw_2001,p_hw_2001,p_uf_2001,p_ff_2001,p_ags_2001, + n_dep_2002,n_ff_2002,n_uf_2002,n_lw_2002,n_hw_2002,n_ags_2002,n_cf_2002,n_cr_2002,p_cr_2002,p_lw_2002,p_hw_2002,p_uf_2002,p_ff_2002,p_ags_2002, + n_dep_2003,n_ff_2003,n_uf_2003,n_lw_2003,n_hw_2003,n_ags_2003,n_cf_2003,n_cr_2003,p_cr_2003,p_lw_2003,p_hw_2003,p_uf_2003,p_ff_2003,p_ags_2003, + n_dep_2004,n_ff_2004,n_uf_2004,n_lw_2004,n_hw_2004,n_ags_2004,n_cf_2004,n_cr_2004,p_cr_2004,p_lw_2004,p_hw_2004,p_uf_2004,p_ff_2004,p_ags_2004, + n_dep_2005,n_ff_2005,n_uf_2005,n_lw_2005,n_hw_2005,n_ags_2005,n_cf_2005,n_cr_2005,p_cr_2005,p_lw_2005,p_hw_2005,p_uf_2005,p_ff_2005,p_ags_2005, + n_dep_2006,n_ff_2006,n_uf_2006,n_lw_2006,n_hw_2006,n_ags_2006,n_cf_2006,n_cr_2006,p_cr_2006,p_lw_2006,p_hw_2006,p_uf_2006,p_ff_2006,p_ags_2006, + n_dep_2007,n_ff_2007,n_uf_2007,n_lw_2007,n_hw_2007,n_ags_2007,n_cf_2007,n_cr_2007,p_cr_2007,p_lw_2007,p_hw_2007,p_uf_2007,p_ff_2007,p_ags_2007, + n_dep_2008,n_ff_2008,n_uf_2008,n_lw_2008,n_hw_2008,n_ags_2008,n_cf_2008,n_cr_2008,p_cr_2008,p_lw_2008,p_hw_2008,p_uf_2008,p_ff_2008,p_ags_2008, + n_dep_2009,n_ff_2009,n_uf_2009,n_lw_2009,n_hw_2009,n_ags_2009,n_cf_2009,n_cr_2009,p_cr_2009,p_lw_2009,p_hw_2009,p_uf_2009,p_ff_2009,p_ags_2009, + n_dep_2010,n_ff_2010,n_uf_2010,n_lw_2010,n_hw_2010,n_ags_2010,n_cf_2010,n_cr_2010,p_cr_2010,p_lw_2010,p_hw_2010,p_uf_2010,p_ff_2010,p_ags_2010, + n_dep_2011,n_ff_2011,n_uf_2011,n_lw_2011,n_hw_2011,n_ags_2011,n_cf_2011,n_cr_2011,p_cr_2011,p_lw_2011,p_hw_2011,p_uf_2011,p_ff_2011,p_ags_2011, + n_dep_2012,n_ff_2012,n_uf_2012,n_lw_2012,n_hw_2012,n_ags_2012,n_cf_2012,n_cr_2012,p_cr_2012,p_lw_2012,p_hw_2012,p_uf_2012,p_ff_2012,p_ags_2012, + n_dep_2013,n_ff_2013,n_uf_2013,n_lw_2013,n_hw_2013,n_ags_2013,n_cf_2013,n_cr_2013,p_cr_2013,p_lw_2013,p_hw_2013,p_uf_2013,p_ff_2013,p_ags_2013, + n_dep_2014,n_ff_2014,n_uf_2014,n_lw_2014,n_hw_2014,n_ags_2014,n_cf_2014,n_cr_2014,p_cr_2014,p_lw_2014,p_hw_2014,p_uf_2014,p_ff_2014,p_ags_2014, + n_dep_2015,n_ff_2015,n_uf_2015,n_lw_2015,n_hw_2015,n_ags_2015,n_cf_2015,n_cr_2015,p_cr_2015,p_lw_2015,p_hw_2015,p_uf_2015,p_ff_2015,p_ags_2015, + n_dep_2016,n_ff_2016,n_uf_2016,n_lw_2016,n_hw_2016,n_ags_2016,n_cf_2016,n_cr_2016,p_cr_2016,p_lw_2016,p_hw_2016,p_uf_2016,p_ff_2016,p_ags_2016, + n_dep_2017,n_ff_2017,n_uf_2017,n_lw_2017,n_hw_2017,n_ags_2017,n_cf_2017,n_cr_2017,p_cr_2017,p_lw_2017,p_hw_2017,p_uf_2017,p_ff_2017,p_ags_2017', + aoi='ws', + comid = com, + showAreaSqKm = FALSE, + showPctFull = FALSE) + + #Create N inputs df + + nin <- nni[, grepl("^(n)", names(nni)) & !grepl("(cr)", names(nni)) & !grepl("(ags)", names(nni))] + + names(nin) <- sapply(names(nin), function(col){ + substr(col, 3, nchar(col) -2) + }) + + nin <- nin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) + + #Create P inputs df + + pin <- nni[, grepl("^(p)", names(nni)) & !grepl("(cr)", names(nni)) & !grepl("(ags)", names(nni))] + + names(pin) <- sapply(names(pin), function(col){ + substr(col, 3, nchar(col) -2) + }) + + pin <- pin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) + + #Create N dfs for lines (cr, agsur, nue) + + nlines <- nni[, grepl("^n_cr|^n_ags", names(nni))] + + names(nlines) <- sapply(names(nlines), function(col){ + substr(col, 3, nchar(col) -2) + }) + + nlines <- nlines %>% + pivot_longer( + cols = everything(), + names_to = c("metric","year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) %>% + pivot_wider( + names_from = 'metric', + values_from = 'value' + ) %>% + mutate(totag = ags + cr) %>% + mutate(nue = (cr / totag) * 100) %>% + pivot_longer( + cols = !year, + names_to="metric", + values_to="value") + + ncrag <- nlines %>% + filter(metric %in% c('ags', 'cr')) + + nue <- nlines %>% + filter(metric == 'nue') %>% + pivot_wider(names_from = 'metric', + values_from = 'value') + + #Create P dfs for lines (cr, agsur, pue) + + plines <- nni[, grepl("^p_cr|^p_ags", names(nni))] + + names(plines) <- sapply(names(plines), function(col){ + substr(col, 3, nchar(col) -2) + }) + + + plines <- plines %>% + pivot_longer( + cols = everything(), + names_to = c("metric","year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) %>% + mutate(value = value / 1000000) %>% + pivot_wider( + names_from = 'metric', + values_from = 'value' + ) %>% + mutate(totag = ags + cr) %>% + mutate(nue = (cr / totag) * 100) %>% + pivot_longer( + cols = !year, + names_to="metric", + values_to="value") + + pcrag <- plines %>% + filter(metric %in% c('ags', 'cr')) + + pue <- plines %>% + filter(metric == 'nue') %>% + pivot_wider(names_from = 'metric', + values_from = 'value') + + pdf <- bind_rows(plines, pin) + + ndf <- bind_rows(nlines, nin) + + #create estimate column + knownfertyrs <- c(1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004, + 2005,2006,2007,2008,2009,2010,2011,2012,2017) + nwsin <- nin %>% + mutate(estimated=case_when( + metric == "dep" ~ FALSE, + metric == "hw" ~ FALSE, + metric == "cf" & year %in% c(1987,1992,1997,2002,2007,2012, 2017) ~ FALSE, + metric == "ff" & year %in% knownfertyrs ~ FALSE, + metric == "uf" & year %in% knownfertyrs ~ FALSE, + metric == "lw" & year %in% c(1987,1992,1997,2002,2007,2012,2017) ~ FALSE, + TRUE ~ TRUE + )) + + pwsin <- pin %>% + filter(metric != 'cr') %>% + mutate(estimated=case_when( + metric == "hw" ~ FALSE, + metric == "lw" & year %in% c(1987,1992,1997,2002,2007,2012,2017) ~ FALSE, + metric == "ff" & year %in% knownfertyrs ~ FALSE, + metric == "uf" & year %in% knownfertyrs ~ FALSE, + TRUE ~ TRUE + )) + + #get ready for plot + colorsn <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black', 'dep'='#6db6ff', 'cf'='#FFD700') + colorsp <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black') + + nwsin$metric <- factor(nwsin$metric, levels = c('uf','hw','dep','lw','cf','ff')) + pwsin$metric <- factor(pwsin$metric, levels = c('uf','hw','lw','ff')) + + + #create titles with higher level include.nue param + if (include.nue == TRUE){ + nbartitle <- 'b)' + pbartitle <- 'd)' + nuetitle <- 'a)' + puetitle <- 'c)' + } else{ + nbartitle <- 'a)' + pbartitle <- 'b)' + nuetitle <- ' ' + puetitle <- ' ' + } + + #create N bar plot + nbar <- ggplot() + + geom_bar_pattern(data = nwsin, + aes(x=year,y=value, fill=metric, + pattern=factor(estimated, levels=c(TRUE,FALSE), + labels=c('Estimated','Non-Estimated'))), + pattern = ifelse(nwsin$estimated, 'stripe','none'), + pattern_color='white', + pattern_density=0.05, + pattern_fill = 'white', + pattern_alpha = 0.5, + pattern_spacing=0.025, + stat='identity', position='stack', + pattern_size=0.05) + + labs(title = nbartitle, + y = "Inputs, Crop Removal, Surplus (million kg)", + x = " ") + + scale_fill_manual(values=colorsn, + labels = c('ff' = 'Farm Fertilizer', + 'uf' = 'Urban Fertilizer', + 'cf' = 'Crop N-Fixation', + 'lw' = 'Livestock Manure', + 'hw' = 'Human Waste', + 'dep' = 'Total Deposition')) + + scale_pattern_manual(name='Estimate Status', + values=c('Estimated'='stripe','Non-estimated'='none')) + + geom_line(data=ncrag, + aes(x=year,y=value, linetype=metric), + linewidth=1.25, color="black") + + scale_linetype_manual(values = + c("ags"="solid", "cr"="dotted"), + labels = c('ags' = 'Agricultural Surplus', + 'cr' = 'Crop Removal')) + + guides(fill= + guide_legend(order=1, override.aes = list(pattern='none')), + pattern= + guide_legend(order=2, override.aes = list(fill='grey')), + linetype= + guide_legend(title=NULL)) + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + scale_color_manual(values=c("Agricultural Surplus"="black"), + guide = 'none') + + theme_bw() + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.y = element_text(size=9), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + #create p bar plot + pbar <- ggplot() + + geom_bar_pattern(data = pwsin, + aes(x=year,y=value, fill=metric, + pattern=factor(estimated, levels=c(TRUE,FALSE), + labels=c('Estimated','Non-Estimated'))), + pattern = ifelse(pwsin$estimated, 'stripe','none'), + pattern_color='white', + pattern_density=0.05, + pattern_fill = 'white', + pattern_alpha = 0.5, + pattern_spacing=0.025, + stat='identity', position='stack', pattern_size=0.05) + + labs(title = pbartitle, + y = "Inputs, Crop Removal, Surplus (million kg)", + x = " ") + + scale_fill_manual(values=colorsp) + + scale_pattern_manual(name='Estimate Status', + values=c('Estimated'='stripe', + 'Non-estimated'='none')) + + geom_line(data=pcrag, + aes(x=year,y=value, + linetype=metric), + linewidth=1.25, color="black") + + scale_linetype_manual(values = + c("ags"="solid", "cr"="dotted")) + + guides(fill= + guide_legend(order=1, override.aes = list(pattern='none')), + pattern= + guide_legend(order=2, override.aes = list(fill='grey')), + linetype= + guide_legend(title=NULL)) + + guides(fill="none", pattern = "none", linetype="none") + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + scale_color_manual(values=c("Agricultural Surplus"="black"), + guide = 'none') + + theme_bw() + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.y = element_text(size=9), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + #create nue line plots + nue <- ggplot() + + geom_line(data=nue, aes(x=year,y=nue), linewidth=1.25, color='seagreen')+ + theme_bw() + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + labs(title = nuetitle, + y = "%", + x=" ") + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.x = element_text(size=9), + axis.title.y = element_text(size=9)) + + pue <- ggplot() + + geom_line(data=pue, aes(x=year,y=nue, lty='Nutrient Use Efficiency'), linewidth=1.25, color="seagreen") + + theme_bw() + + scale_x_continuous(breaks=seq(1987,2017,by=5)) + + labs(title = puetitle, + y = "%", + x="Year") + + theme(plot.title = element_text(size=9, face="bold"), + axis.title.x = element_text(size=9), + axis.title.y = element_text(size=9, hjust=0.5), + legend.background = element_rect(fill="white", colour = "black"), + legend.title = element_blank()) + + guides(fill="none", pattern = "none", linetype="none") + + #export final figure + inputs <- (nbar / pbar) + plot_layout(guides = "collect") + nue <- (nue / pue) + plot_layout(guides = "collect") + + if (include.nue == TRUE){ + timenni <- (nue | inputs) + } + else { + timenni <- inputs + } + + return(timenni) + +} From 759bc51169cc5505298f733fed8a02b7d4e37ced Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:40:36 -0700 Subject: [PATCH 09/23] new functions for sc_plot and lc_plot codes --- DESCRIPTION | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 82e4a72..5d3dbda 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -49,7 +49,10 @@ Suggests: readr, tidyr, stringr, - purrr + purrr, + ggpattern, + patchwork, + cowplot Encoding: UTF-8 URL: https://usepa.github.io/StreamCatTools/, https://github.com/USEPA/StreamCatTools BugReports: https://github.com/USEPA/StreamCatTools/issues From 226ca91eb7c4bf3116a37003e9a415f2edc98e1e Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:42:26 -0700 Subject: [PATCH 10/23] updates to examples --- R/sc_get_data.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/sc_get_data.R b/R/sc_get_data.R index 92575cc..6d165c9 100644 --- a/R/sc_get_data.R +++ b/R/sc_get_data.R @@ -341,12 +341,12 @@ ignore_unused_imports <- function() { #' #' @examples\donttest{ #' df <- sc_getnni(year='1987, 1990, 2005, 2017', aoi='cat,ws', -#' comid='23783629,23794487,23812618') +#' comid=179,1337,1337420') #' #' df <- sc_getnni(year='2015', aoi='cat', -#' comid='23783629', countOnly=TRUE) +#' comid='179', countOnly=TRUE) #' -#' df <- sc_getnni(comid='23783629', year='2011, 2012', aoi='ws') +#' df <- sc_getnni(comid='179', year='2011, 2012', aoi='ws') #' } sc_get_nni <- function(year, aoi = NULL, comid = NULL, From d8de1fe5e0efc59e352568840df65fd9ae130c94 Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:42:53 -0700 Subject: [PATCH 11/23] some changes to plot functions --- R/lc_plot.R | 12 +++--- R/sc_plot.R | 111 ++++++++++++++++++++++++++-------------------------- 2 files changed, 61 insertions(+), 62 deletions(-) diff --git a/R/lc_plot.R b/R/lc_plot.R index b44cc08..2f9d89e 100644 --- a/R/lc_plot.R +++ b/R/lc_plot.R @@ -207,8 +207,8 @@ lc_plotnni <- function(comid, include.nue = FALSE){ pattern_spacing=0.025, stat='identity', position='stack', pattern_size=0.05) + - labs(title = nbartitle, - y = "Inputs, Crop Removal, Surplus (million kg)", + labs(title = 'Nitrogen (million kg)', + y = "Budget", x = " ") + scale_fill_manual(values=colorsn, labels = c('ff' = 'Farm Fertilizer', @@ -254,8 +254,8 @@ lc_plotnni <- function(comid, include.nue = FALSE){ pattern_alpha = 0.5, pattern_spacing=0.025, stat='identity', position='stack', pattern_size=0.05) + - labs(title = pbartitle, - y = "Inputs, Crop Removal, Surplus (million kg)", + labs(title = 'Phosphorus (million kg)', + y = "Budget", x = " ") + scale_fill_manual(values=colorsp) + scale_pattern_manual(name='Estimate Status', @@ -288,7 +288,7 @@ lc_plotnni <- function(comid, include.nue = FALSE){ geom_line(data=nue, aes(x=year,y=nue), linewidth=1.25, color='seagreen')+ theme_bw() + scale_x_continuous(breaks=seq(1987,2017,by=5)) + - labs(title = nuetitle, + labs(title = 'Nitrogen Use Efficiency', y = "%", x=" ") + theme(plot.title = element_text(size=9, face="bold"), @@ -299,7 +299,7 @@ lc_plotnni <- function(comid, include.nue = FALSE){ geom_line(data=pue, aes(x=year,y=nue, lty='Nutrient Use Efficiency'), linewidth=1.25, color="seagreen") + theme_bw() + scale_x_continuous(breaks=seq(1987,2017,by=5)) + - labs(title = puetitle, + labs(title = 'Phosphorus Use Efficiency', y = "%", x="Year") + theme(plot.title = element_text(size=9, face="bold"), diff --git a/R/sc_plot.R b/R/sc_plot.R index a82bc5f..fdae9d8 100644 --- a/R/sc_plot.R +++ b/R/sc_plot.R @@ -14,7 +14,11 @@ #' Syntax: com= #' #' @param include.nue Include time series of nitrogen use efficiency in the returned plot. -#' The default value is false. +#' The default value is true. +#' Values: true|false +#' +#' @param include.inset Include inset map that shows the location of the COMID and its basin. +#' The default value is true. #' Values: true|false #' #' @return @@ -25,42 +29,16 @@ #' \donttest #' p <- sc_plotnni(comid='1337420') #' p <- sc_plotnni(comid='1337420', include.nue=TRUE) +#' p <- sc_plotnni(comid='1337420', include.inset=FALSE) -sc_plotnni <- function(com, include.nue = FALSE){ +sc_plotnni <- function(comid, include.nue = FALSE, include.inset = TRUE){ # Get StreamCat data - nni <- sc_get_data(metric = 'n_dep_1990,n_ff_1990,n_uf_1990,n_lw_1990,n_hw_1990,n_ags_1990,n_cf_1990,n_cr_1990,p_cr_1990,p_lw_1990,p_hw_1990,p_uf_1990,p_ff_1990, - p_ags_1990,n_dep_1991,n_ff_1991,n_uf_1991,n_lw_1991,n_hw_1991,n_ags_1991,n_cf_1991,n_cr_1991,p_cr_1991,p_lw_1991,p_hw_1991,p_uf_1991,p_ff_1991,p_ags_1991, - n_dep_1992,n_ff_1992,n_uf_1992,n_lw_1992,n_hw_1992,n_ags_1992,n_cf_1992,n_cr_1992,p_cr_1992,p_lw_1992,p_hw_1992,p_uf_1992,p_ff_1992,p_ags_1992, - n_dep_1993,n_ff_1993,n_uf_1993,n_lw_1993,n_hw_1993,n_ags_1993,n_cf_1993,n_cr_1993,p_cr_1993,p_lw_1993,p_hw_1993,p_uf_1993,p_ff_1993,p_ags_1993, - n_dep_1994,n_ff_1994,n_uf_1994,n_lw_1994,n_hw_1994,n_ags_1994,n_cf_1994,n_cr_1994,p_cr_1994,p_lw_1994,p_hw_1994,p_uf_1994,p_ff_1994,p_ags_1994, - n_dep_1995,n_ff_1995,n_uf_1995,n_lw_1995,n_hw_1995,n_ags_1995,n_cf_1995,n_cr_1995,p_cr_1995,p_lw_1995,p_hw_1995,p_uf_1995,p_ff_1995,p_ags_1995, - n_dep_1996,n_ff_1996,n_uf_1996,n_lw_1996,n_hw_1996,n_ags_1996,n_cf_1996,n_cr_1996,p_cr_1996,p_lw_1996,p_hw_1996,p_uf_1996,p_ff_1996,p_ags_1996, - n_dep_1997,n_ff_1997,n_uf_1997,n_lw_1997,n_hw_1997,n_ags_1997,n_cf_1997,n_cr_1997,p_cr_1997,p_lw_1997,p_hw_1997,p_uf_1997,p_ff_1997,p_ags_1997, - n_dep_1998,n_ff_1998,n_uf_1998,n_lw_1998,n_hw_1998,n_ags_1998,n_cf_1998,n_cr_1998,p_cr_1998,p_lw_1998,p_hw_1998,p_uf_1998,p_ff_1998,p_ags_1998, - n_dep_1999,n_ff_1999,n_uf_1999,n_lw_1999,n_hw_1999,n_ags_1999,n_cf_1999,n_cr_1999,p_cr_1999,p_lw_1999,p_hw_1999,p_uf_1999,p_ff_1999,p_ags_1999, - n_dep_2000,n_ff_2000,n_uf_2000,n_lw_2000,n_hw_2000,n_ags_2000,n_cf_2000,n_cr_2000,p_cr_2000,p_lw_2000,p_hw_2000,p_uf_2000,p_ff_2000,p_ags_2000, - n_dep_2001,n_ff_2001,n_uf_2001,n_lw_2001,n_hw_2001,n_ags_2001,n_cf_2001,n_cr_2001,p_cr_2001,p_lw_2001,p_hw_2001,p_uf_2001,p_ff_2001,p_ags_2001, - n_dep_2002,n_ff_2002,n_uf_2002,n_lw_2002,n_hw_2002,n_ags_2002,n_cf_2002,n_cr_2002,p_cr_2002,p_lw_2002,p_hw_2002,p_uf_2002,p_ff_2002,p_ags_2002, - n_dep_2003,n_ff_2003,n_uf_2003,n_lw_2003,n_hw_2003,n_ags_2003,n_cf_2003,n_cr_2003,p_cr_2003,p_lw_2003,p_hw_2003,p_uf_2003,p_ff_2003,p_ags_2003, - n_dep_2004,n_ff_2004,n_uf_2004,n_lw_2004,n_hw_2004,n_ags_2004,n_cf_2004,n_cr_2004,p_cr_2004,p_lw_2004,p_hw_2004,p_uf_2004,p_ff_2004,p_ags_2004, - n_dep_2005,n_ff_2005,n_uf_2005,n_lw_2005,n_hw_2005,n_ags_2005,n_cf_2005,n_cr_2005,p_cr_2005,p_lw_2005,p_hw_2005,p_uf_2005,p_ff_2005,p_ags_2005, - n_dep_2006,n_ff_2006,n_uf_2006,n_lw_2006,n_hw_2006,n_ags_2006,n_cf_2006,n_cr_2006,p_cr_2006,p_lw_2006,p_hw_2006,p_uf_2006,p_ff_2006,p_ags_2006, - n_dep_2007,n_ff_2007,n_uf_2007,n_lw_2007,n_hw_2007,n_ags_2007,n_cf_2007,n_cr_2007,p_cr_2007,p_lw_2007,p_hw_2007,p_uf_2007,p_ff_2007,p_ags_2007, - n_dep_2008,n_ff_2008,n_uf_2008,n_lw_2008,n_hw_2008,n_ags_2008,n_cf_2008,n_cr_2008,p_cr_2008,p_lw_2008,p_hw_2008,p_uf_2008,p_ff_2008,p_ags_2008, - n_dep_2009,n_ff_2009,n_uf_2009,n_lw_2009,n_hw_2009,n_ags_2009,n_cf_2009,n_cr_2009,p_cr_2009,p_lw_2009,p_hw_2009,p_uf_2009,p_ff_2009,p_ags_2009, - n_dep_2010,n_ff_2010,n_uf_2010,n_lw_2010,n_hw_2010,n_ags_2010,n_cf_2010,n_cr_2010,p_cr_2010,p_lw_2010,p_hw_2010,p_uf_2010,p_ff_2010,p_ags_2010, - n_dep_2011,n_ff_2011,n_uf_2011,n_lw_2011,n_hw_2011,n_ags_2011,n_cf_2011,n_cr_2011,p_cr_2011,p_lw_2011,p_hw_2011,p_uf_2011,p_ff_2011,p_ags_2011, - n_dep_2012,n_ff_2012,n_uf_2012,n_lw_2012,n_hw_2012,n_ags_2012,n_cf_2012,n_cr_2012,p_cr_2012,p_lw_2012,p_hw_2012,p_uf_2012,p_ff_2012,p_ags_2012, - n_dep_2013,n_ff_2013,n_uf_2013,n_lw_2013,n_hw_2013,n_ags_2013,n_cf_2013,n_cr_2013,p_cr_2013,p_lw_2013,p_hw_2013,p_uf_2013,p_ff_2013,p_ags_2013, - n_dep_2014,n_ff_2014,n_uf_2014,n_lw_2014,n_hw_2014,n_ags_2014,n_cf_2014,n_cr_2014,p_cr_2014,p_lw_2014,p_hw_2014,p_uf_2014,p_ff_2014,p_ags_2014, - n_dep_2015,n_ff_2015,n_uf_2015,n_lw_2015,n_hw_2015,n_ags_2015,n_cf_2015,n_cr_2015,p_cr_2015,p_lw_2015,p_hw_2015,p_uf_2015,p_ff_2015,p_ags_2015, - n_dep_2016,n_ff_2016,n_uf_2016,n_lw_2016,n_hw_2016,n_ags_2016,n_cf_2016,n_cr_2016,p_cr_2016,p_lw_2016,p_hw_2016,p_uf_2016,p_ff_2016,p_ags_2016, - n_dep_2017,n_ff_2017,n_uf_2017,n_lw_2017,n_hw_2017,n_ags_2017,n_cf_2017,n_cr_2017,p_cr_2017,p_lw_2017,p_hw_2017,p_uf_2017,p_ff_2017,p_ags_2017', - aoi='ws', - comid = com, - showAreaSqKm = FALSE, - showPctFull = FALSE) + nni <- sc_get_data(metric = 'n_dep_1990,n_ff_1990,n_uf_1990,n_lw_1990,n_hw_1990,n_ags_1990,n_cf_1990,n_cr_1990,p_cr_1990,p_lw_1990,p_hw_1990,p_uf_1990,p_ff_1990,p_ags_1990,n_dep_1991,n_ff_1991,n_uf_1991,n_lw_1991,n_hw_1991,n_ags_1991,n_cf_1991,n_cr_1991,p_cr_1991,p_lw_1991,p_hw_1991,p_uf_1991,p_ff_1991,p_ags_1991,n_dep_1992,n_ff_1992,n_uf_1992,n_lw_1992,n_hw_1992,n_ags_1992,n_cf_1992,n_cr_1992,p_cr_1992,p_lw_1992,p_hw_1992,p_uf_1992,p_ff_1992,p_ags_1992,n_dep_1993,n_ff_1993,n_uf_1993,n_lw_1993,n_hw_1993,n_ags_1993,n_cf_1993,n_cr_1993,p_cr_1993,p_lw_1993,p_hw_1993,p_uf_1993,p_ff_1993,p_ags_1993,n_dep_1994,n_ff_1994,n_uf_1994,n_lw_1994,n_hw_1994,n_ags_1994,n_cf_1994,n_cr_1994,p_cr_1994,p_lw_1994,p_hw_1994,p_uf_1994,p_ff_1994,p_ags_1994,n_dep_1995,n_ff_1995,n_uf_1995,n_lw_1995,n_hw_1995,n_ags_1995,n_cf_1995,n_cr_1995,p_cr_1995,p_lw_1995,p_hw_1995,p_uf_1995,p_ff_1995,p_ags_1995,n_dep_1996,n_ff_1996,n_uf_1996,n_lw_1996,n_hw_1996,n_ags_1996,n_cf_1996,n_cr_1996,p_cr_1996,p_lw_1996,p_hw_1996,p_uf_1996,p_ff_1996,p_ags_1996,n_dep_1997,n_ff_1997,n_uf_1997,n_lw_1997,n_hw_1997,n_ags_1997,n_cf_1997,n_cr_1997,p_cr_1997,p_lw_1997,p_hw_1997,p_uf_1997,p_ff_1997,p_ags_1997,n_dep_1998,n_ff_1998,n_uf_1998,n_lw_1998,n_hw_1998,n_ags_1998,n_cf_1998,n_cr_1998,p_cr_1998,p_lw_1998,p_hw_1998,p_uf_1998,p_ff_1998,p_ags_1998,n_dep_1999,n_ff_1999,n_uf_1999,n_lw_1999,n_hw_1999,n_ags_1999,n_cf_1999,n_cr_1999,p_cr_1999,p_lw_1999,p_hw_1999,p_uf_1999,p_ff_1999,p_ags_1999,n_dep_2000,n_ff_2000,n_uf_2000,n_lw_2000,n_hw_2000,n_ags_2000,n_cf_2000,n_cr_2000,p_cr_2000,p_lw_2000,p_hw_2000,p_uf_2000,p_ff_2000,p_ags_2000,n_dep_2001,n_ff_2001,n_uf_2001,n_lw_2001,n_hw_2001,n_ags_2001,n_cf_2001,n_cr_2001,p_cr_2001,p_lw_2001,p_hw_2001,p_uf_2001,p_ff_2001,p_ags_2001,n_dep_2002,n_ff_2002,n_uf_2002,n_lw_2002,n_hw_2002,n_ags_2002,n_cf_2002,n_cr_2002,p_cr_2002,p_lw_2002,p_hw_2002,p_uf_2002,p_ff_2002,p_ags_2002,n_dep_2003,n_ff_2003,n_uf_2003,n_lw_2003,n_hw_2003,n_ags_2003,n_cf_2003,n_cr_2003,p_cr_2003,p_lw_2003,p_hw_2003,p_uf_2003,p_ff_2003,p_ags_2003,n_dep_2004,n_ff_2004,n_uf_2004,n_lw_2004,n_hw_2004,n_ags_2004,n_cf_2004,n_cr_2004,p_cr_2004,p_lw_2004,p_hw_2004,p_uf_2004,p_ff_2004,p_ags_2004,n_dep_2005,n_ff_2005,n_uf_2005,n_lw_2005,n_hw_2005,n_ags_2005,n_cf_2005,n_cr_2005,p_cr_2005,p_lw_2005,p_hw_2005,p_uf_2005,p_ff_2005,p_ags_2005,n_dep_2006,n_ff_2006,n_uf_2006,n_lw_2006,n_hw_2006,n_ags_2006,n_cf_2006,n_cr_2006,p_cr_2006,p_lw_2006,p_hw_2006,p_uf_2006,p_ff_2006,p_ags_2006,n_dep_2007,n_ff_2007,n_uf_2007,n_lw_2007,n_hw_2007,n_ags_2007,n_cf_2007,n_cr_2007,p_cr_2007,p_lw_2007,p_hw_2007,p_uf_2007,p_ff_2007,p_ags_2007,n_dep_2008,n_ff_2008,n_uf_2008,n_lw_2008,n_hw_2008,n_ags_2008,n_cf_2008,n_cr_2008,p_cr_2008,p_lw_2008,p_hw_2008,p_uf_2008,p_ff_2008,p_ags_2008,n_dep_2009,n_ff_2009,n_uf_2009,n_lw_2009,n_hw_2009,n_ags_2009,n_cf_2009,n_cr_2009,p_cr_2009,p_lw_2009,p_hw_2009,p_uf_2009,p_ff_2009,p_ags_2009,n_dep_2010,n_ff_2010,n_uf_2010,n_lw_2010,n_hw_2010,n_ags_2010,n_cf_2010,n_cr_2010,p_cr_2010,p_lw_2010,p_hw_2010,p_uf_2010,p_ff_2010,p_ags_2010,n_dep_2011,n_ff_2011,n_uf_2011,n_lw_2011,n_hw_2011,n_ags_2011,n_cf_2011,n_cr_2011,p_cr_2011,p_lw_2011,p_hw_2011,p_uf_2011,p_ff_2011,p_ags_2011,n_dep_2012,n_ff_2012,n_uf_2012,n_lw_2012,n_hw_2012,n_ags_2012,n_cf_2012,n_cr_2012,p_cr_2012,p_lw_2012,p_hw_2012,p_uf_2012,p_ff_2012,p_ags_2012,n_dep_2013,n_ff_2013,n_uf_2013,n_lw_2013,n_hw_2013,n_ags_2013,n_cf_2013,n_cr_2013,p_cr_2013,p_lw_2013,p_hw_2013,p_uf_2013,p_ff_2013,p_ags_2013,n_dep_2014,n_ff_2014,n_uf_2014,n_lw_2014,n_hw_2014,n_ags_2014,n_cf_2014,n_cr_2014,p_cr_2014,p_lw_2014,p_hw_2014,p_uf_2014,p_ff_2014,p_ags_2014,n_dep_2015,n_ff_2015,n_uf_2015,n_lw_2015,n_hw_2015,n_ags_2015,n_cf_2015,n_cr_2015,p_cr_2015,p_lw_2015,p_hw_2015,p_uf_2015,p_ff_2015,p_ags_2015,n_dep_2016,n_ff_2016,n_uf_2016,n_lw_2016,n_hw_2016,n_ags_2016,n_cf_2016,n_cr_2016,p_cr_2016,p_lw_2016,p_hw_2016,p_uf_2016,p_ff_2016,p_ags_2016,n_dep_2017,n_ff_2017,n_uf_2017,n_lw_2017,n_hw_2017,n_ags_2017,n_cf_2017,n_cr_2017,p_cr_2017,p_lw_2017,p_hw_2017,p_uf_2017,p_ff_2017,p_ags_2017', + aoi='ws', + comid = comid, + showAreaSqKm = FALSE, + showPctFull = FALSE) #Create N inputs df @@ -206,19 +184,17 @@ sc_plotnni <- function(com, include.nue = FALSE){ nwsin$metric <- factor(nwsin$metric, levels = c('uf','hw','dep','lw','cf','ff')) pwsin$metric <- factor(pwsin$metric, levels = c('uf','hw','lw','ff')) + #Get COMID location and states for inset map + #states + states <- + tigris::states(cb = TRUE, progress_bar = FALSE) %>% + filter(!STUSPS %in% c('HI', 'PR', 'AK', 'MP', 'GU', 'AS', 'VI')) %>% + st_transform(crs = 5070) - #create titles with higher level include.nue param - if (include.nue == TRUE){ - nbartitle <- 'b)' - pbartitle <- 'd)' - nuetitle <- 'a)' - puetitle <- 'c)' - } else{ - nbartitle <- 'a)' - pbartitle <- 'b)' - nuetitle <- ' ' - puetitle <- ' ' - } + #comid + comidint <- as.integer(comid) + flowline <- get_nhdplus(comid = comidint, realization = "flowline") + point <- st_centroid(flowline) #create N bar plot nbar <- ggplot() + @@ -234,8 +210,8 @@ sc_plotnni <- function(com, include.nue = FALSE){ pattern_spacing=0.025, stat='identity', position='stack', pattern_size=0.05) + - labs(title = nbartitle, - y = "Inputs, Crop Removal, Surplus (million kg)", + labs(title = 'Nitrogen (million kg/year)', + y = "Budget", x = " ") + scale_fill_manual(values=colorsn, labels = c('ff' = 'Farm Fertilizer', @@ -281,8 +257,8 @@ sc_plotnni <- function(com, include.nue = FALSE){ pattern_alpha = 0.5, pattern_spacing=0.025, stat='identity', position='stack', pattern_size=0.05) + - labs(title = pbartitle, - y = "Inputs, Crop Removal, Surplus (million kg)", + labs(title = 'Phosphorus (million kg/year)', + y = "Budget", x = " ") + scale_fill_manual(values=colorsp) + scale_pattern_manual(name='Estimate Status', @@ -315,7 +291,7 @@ sc_plotnni <- function(com, include.nue = FALSE){ geom_line(data=nue, aes(x=year,y=nue), linewidth=1.25, color='seagreen')+ theme_bw() + scale_x_continuous(breaks=seq(1987,2017,by=5)) + - labs(title = nuetitle, + labs(title = 'Nitrogen Use Efficiency', y = "%", x=" ") + theme(plot.title = element_text(size=9, face="bold"), @@ -326,7 +302,7 @@ sc_plotnni <- function(com, include.nue = FALSE){ geom_line(data=pue, aes(x=year,y=nue, lty='Nutrient Use Efficiency'), linewidth=1.25, color="seagreen") + theme_bw() + scale_x_continuous(breaks=seq(1987,2017,by=5)) + - labs(title = puetitle, + labs(title = 'Phosphorus Use Efficiency', y = "%", x="Year") + theme(plot.title = element_text(size=9, face="bold"), @@ -336,17 +312,40 @@ sc_plotnni <- function(com, include.nue = FALSE){ legend.title = element_blank()) + guides(fill="none", pattern = "none", linetype="none") + #Create inset map + inset <- ggplot() + + geom_sf(data = states, color = "grey", fill = 'transparent', lwd = .2) + + geom_sf(data = point, size = 3.5, color = "red") + theme_void() + #export final figure inputs <- (nbar / pbar) + plot_layout(guides = "collect") nue <- (nue / pue) + plot_layout(guides = "collect") if (include.nue == TRUE){ - timenni <- (nue | inputs) + timenni <- (nue | inputs) } else { - timenni <- inputs + timenni <- inputs } - - return(timenni) + + if (include.inset == TRUE){ + #legend <- get_legend( + # timenni + theme(legend.position = "bottom") + #) + #insetlegend <- plot_grid( + # inset, legend, + # ncol = 1, + # rel_heights = c(2,1) + #) + timenni <- plot_grid( + timenni, inset, + ncol = 1, + rel_heights = c(3,1) + ) + return(timenni) + } + else { + return(timenni) + } } From 0222a63bb3af63c0b5d8ec3fc9c0d4ab3f2be2bd Mon Sep 17 00:00:00 2001 From: Markley Date: Mon, 29 Sep 2025 12:55:45 -0700 Subject: [PATCH 12/23] added nni plot and nni get data functions to examples in NNI vignette. Includes 3 ex of plots, and a few examples of data uses. --- vignettes/NNI.Rmd | 157 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 2 deletions(-) diff --git a/vignettes/NNI.Rmd b/vignettes/NNI.Rmd index a87ca05..ba21ee2 100644 --- a/vignettes/NNI.Rmd +++ b/vignettes/NNI.Rmd @@ -26,10 +26,163 @@ knitr::opts_chunk$set( ) ``` -## Access Farm Nitrogen data for a watershed +## Use sc_plotnni and lc_plotnni +In the following examples, we use sc_plotnni and lc_plotnni functions to plot annual time series +of nitrogen and phosphorus budget data for a given watershed. These data are plotted from 1990-2017. Hatching on the bars +is reflective of data linearly interpolated between agricultural census years. + +Mississippi-Atchafalaya River Basin +```{r, warning = FALSE, message = FALSE} +library(StreamCatTools) +library(dplyr) +library(tidyr) +library(ggplot2) +library(ggpattern) +library(patchwork) +library(cowplot) +com <- '22812041' +sc_plotnni(comid = com, include.nue = TRUE) +``` + +Neuse River, North Carolina +```{r, warning = FALSE, message = FAlSE} +com <- '10975909' +sc_plotnni(comid = com) +``` + +Lake Kanasatka, New Hampshire +```{r} +com = '6738112' +lc_plotnni(comid = com) +``` +## Use sc_get_nni and lc_get_nni to pull NNI budget data +In these examples, we access all available NNI metrics for user-defined years. Most NNI metrics are available from 1987 +to 2017, except for nitrogen deposition, phosphorus deposition, and nitrogen and phosphorus point source loads. These metrics +are returned for available years. + +## Cuyahoga River +Return dataframe of NNI metrics for Cuyahoga River +```{r} +com <- 15588532 +years <- '1987, 1992, 1997, 2002, 2007, 2012, 2017' +cuyahoga <- sc_get_nni(year=years, + comid=com, + aoi='ws', + showAreaSqKm=FALSE) +colnames(cuyahoga) +``` +Transform sc_get_nni dataframe into long dataframe +```{r} +nin <- cuyahoga[, grepl("^(n)", names(cuyahoga)) & + !grepl("(cr)", names(cuyahoga)) & + !grepl("(ags)", names(cuyahoga)) & + !grepl("(leg)", names(cuyahoga))] + +names(nin) <- sapply(names(nin), function(col){ + substr(col, 3, nchar(col) -2) +}) + +nin <- nin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) + +pin <- cuyahoga[, grepl("^(p)", names(cuyahoga)) & + !grepl("(cr)", names(cuyahoga)) & + !grepl("(ags)", names(cuyahoga)) & + !grepl("(leg)", names(cuyahoga))] + +names(pin) <- sapply(names(pin), function(col){ + substr(col, 3, nchar(col) -2) +}) + +pin <- pin %>% + pivot_longer( + cols = everything(), + names_to = c("metric", "year"), + names_sep = "_", + values_to = "value" + ) %>% + mutate(year = as.integer(year)) +``` + +Pie Chart of Sources of N and P to the Cuyahoga River in 2012 +```{r} +n2012 <- nin %>% + filter(year == '2012') + +p2012 <- pin %>% + filter(year == '2012') + +colorsn <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black', 'dep'='#6db6ff', 'cf'='#FFD700', 'usgsww' = 'pink') +colorsp <- c('ff' = '#A3CC51', 'lw'='#B26F2C','hw'='#E51932','uf'='black', 'usgsww' = 'pink') + +n <- ggplot(nin, aes(x = "", y = value, fill = metric)) + + geom_bar(stat = "identity", width = 1) + + coord_polar(theta = "y") + + scale_fill_manual(values = colorsn, labels = + c('ff' = 'Farm Fertilizer', + 'uf' = 'Urban Fertilizer', + 'hw' = 'Human Waste', + 'lw' = 'Livestock Waste', + 'dep' = 'Total Deposition', + 'usgsww' = 'Point Source Loads', + 'cf' = 'Crop N-Fixation') + ) + + theme_void() + + labs(title = "Nitrogen", + fill = 'Source') + +p <- ggplot(pin, aes(x = "", y = value, fill = metric)) + + geom_bar(stat = "identity", width = 1) + + coord_polar(theta = "y") + + scale_fill_manual(values = colorsp) + + theme_void() + + labs(title = "Phosphorus") + + guides(fill = 'none') + +n + p + plot_layout(guides = "collect") +``` + +Percent change of N and P inputs from 1992 to 2017 +```{r} +nperc <- nin %>% + filter(year %in% c('1992', '2017')) %>% + pivot_wider(values_from = 'value', + names_from = 'year') +nperc$perchange <- (nperc$`2017` - nperc$`1992`) / nperc$`1992` * 100 +nperc <- nperc %>% + select(metric, perchange) %>% + filter(metric != 'usgsww') %>% + rename('Source' = 'metric', 'Nitrogen Percent Change' = 'perchange') + +pperc <- pin %>% + filter(year %in% c('1992', '2017')) %>% + pivot_wider(values_from = 'value', + names_from = 'year') +pperc$perchange <- (pperc$`2017` - pperc$`1992`) / pperc$`1992` * 100 +pperc <- pperc %>% + select(metric, perchange) %>% + filter(metric != 'usgsww') %>% + rename('Source' = 'metric', 'Phosphorus Percent Change' = 'perchange') + +left_join(nperc, pperc, by = 'Source') +``` + +Local catchment and watershed N and P inputs to the Platte River Basin +```{r} +com <- 17416474 +sc_get_nni(year='2017', comid=com, aoi='cat,ws') +``` + + +## Plot a single NNI metric for a given watershed In this example we access a single National Nutrient Inventory (NNI) metric for the Calapooia River basin using the `sc_get_data` function. We use the `nhdplusTools` library to pull in flowlines and the watershed boundary for the Calapooia River basin, plot the selected NNI metric for the Calapooia River and show the watershed. ```{r farmn, results='hide'} -library(StreamCatTools) start_comid = 23763517 nldi_feature <- list(featureSource = "comid", featureID = start_comid) From 89a4279fdba389fb6f50fb2a35e55227fc7faaac Mon Sep 17 00:00:00 2001 From: Marc Weber Date: Thu, 13 Nov 2025 09:24:14 -0800 Subject: [PATCH 13/23] Updates to Vignettes Patch (#84) * Develop (#78) * Create coverage.yaml Init commit * Update coverage.yaml Make sure the report name is specified * Update coverage.yaml Try to get report from ./cobertura.xml * Update coverage.yaml Not there... but try being specific * Update coverage.yaml In case . is somewhere else be specific about where to write * Update coverage.yaml Simplify * Update coverage.yaml Point action to correct coverage report file * Update coverage.yaml Attempt permissions route to post comment vs PAT * Trying Jeff's epar package to generate code cov badge * incrementing dev version * Metric categories (#79) * updated params metrics function and added dontrun for examples * test passing with updates to sc and lc params functions and new sc and lc get_metric_names functions * Updating version * changes to sc_get_params and lc_get_params * fix to allow passing vectors of metric, state, county, region to both sc_get_data and sc_get_data * remove global variables messages * couple more updates to get_params functions and adding Mike as contributor * updated documentation * Increment develop branch * added try catch to functions to fail gracefully and fixed column separator in get_metric_names * added new get_metric_names example to Vignettes and last fixes to tryCatch in functions * document new functions * updated CRAN comments * Ordered vignettes in github pages * handling in vignettes for web service being down * updated CRAN comments * Updated NEWS and DESCRIPTION * updated CRAN comments * Switched most vignettes to articles * updated CRAN comments and NEWS --------- Co-authored-by: Justin Bousquin Co-authored-by: Michael Dumelle --- DESCRIPTION | 2 +- NEWS.md | 14 +- R/sc_get_params.R | 20 +- _pkgdown.yml | 7 +- cran-comments.md | 34 +- docs/404.html | 13 +- docs/LICENSE.html | 11 +- docs/articles/Applications.html | 4 + docs/articles/Articles/Applications.html | 189 + .../HomeButton-0.0.1/LICENSE | 7 + .../HomeButton-0.0.1/easy-button-src.min.js | 1 + .../HomeButton-0.0.1/glyphicons-21-home.png | Bin 0 -> 1266 bytes .../HomeButton-0.0.1/home-button.js | 40 + .../Proj4Leaflet-1.0.1/proj4leaflet.js | 272 + .../basin-CSS-0.0.1/basin_home-button.css | 3 + .../clipboard-0.0.1/setClipboardText.js | 51 + .../figure-html/nars_ggplot-1.png | Bin 0 -> 50652 bytes ...wline_nldi___PCTIMP2011CAT_home-button.css | 3 + .../htmltools-fill-0.5.8.1/fill.css | 21 + .../htmlwidgets-1.6.4/htmlwidgets.js | 901 ++ .../leaflet-1.3.1/images/layers-2x.png | Bin 0 -> 1259 bytes .../leaflet-1.3.1/images/layers.png | Bin 0 -> 696 bytes .../leaflet-1.3.1/images/marker-icon-2x.png | Bin 0 -> 2464 bytes .../leaflet-1.3.1/images/marker-icon.png | Bin 0 -> 1466 bytes .../leaflet-1.3.1/images/marker-shadow.png | Bin 0 -> 618 bytes .../leaflet-1.3.1/leaflet.css | 636 ++ .../leaflet-1.3.1/leaflet.js | 5 + .../leaflet-binding-2.2.3/leaflet.js | 2787 ++++++ .../leaflet-providers_2.0.0.js | 1178 +++ .../leaflet-providers-plugin.js | 3 + .../leafletfix-1.0.0/leafletfix.css | 36 + .../mapviewCSS-0.0.1/mapview-popup.css | 102 + .../mapviewCSS-0.0.1/mapview.css | 1 + .../proj4-2.6.2/proj4.min.js | 1 + .../rstudio_leaflet-1.3.1/images/1px.png | Bin 0 -> 68 bytes .../rstudio_leaflet-1.3.1/rstudio_leaflet.css | 41 + docs/articles/Articles/Introduction.html | 2627 ++++++ docs/articles/Articles/LakeCat.html | 428 + docs/articles/Introduction.html | 4 + docs/articles/LakeCat.html | 4 + docs/articles/StartHere.html | 174 + docs/articles/index.html | 19 +- docs/authors.html | 15 +- docs/deps/bootstrap-5.3.1/bootstrap.min.css | 2 +- docs/deps/data-deps.txt | 4 +- docs/deps/font-awesome-6.5.2/css/all.css | 8028 +++++++++++++++++ docs/deps/font-awesome-6.5.2/css/all.min.css | 9 + docs/deps/font-awesome-6.5.2/css/v4-shims.css | 2194 +++++ .../font-awesome-6.5.2/css/v4-shims.min.css | 6 + .../webfonts/fa-brands-400.ttf | Bin 0 -> 209128 bytes .../webfonts/fa-brands-400.woff2 | Bin 0 -> 117852 bytes .../webfonts/fa-regular-400.ttf | Bin 0 -> 67860 bytes .../webfonts/fa-regular-400.woff2 | Bin 0 -> 25392 bytes .../webfonts/fa-solid-900.ttf | Bin 0 -> 420332 bytes .../webfonts/fa-solid-900.woff2 | Bin 0 -> 156400 bytes .../webfonts/fa-v4compatibility.ttf | Bin 0 -> 10832 bytes .../webfonts/fa-v4compatibility.woff2 | Bin 0 -> 4792 bytes docs/index.html | 13 +- docs/news/index.html | 17 +- docs/pkgdown.yml | 9 +- docs/reference/index.html | 11 +- docs/reference/lc_fullname.html | 11 +- docs/reference/lc_get_comid.html | 11 +- docs/reference/lc_get_data.html | 11 +- docs/reference/lc_get_metric_names.html | 11 +- docs/reference/lc_get_params.html | 11 +- docs/reference/lc_nlcd.html | 11 +- docs/reference/sc_fullname.html | 11 +- docs/reference/sc_get_comid.html | 11 +- docs/reference/sc_get_data.html | 11 +- docs/reference/sc_get_metric_names.html | 11 +- docs/reference/sc_get_params.html | 11 +- docs/reference/sc_nlcd.html | 11 +- docs/search.json | 2 +- docs/sitemap.xml | 4 + tests/testthat/test-lc_get_params.R | 7 + tests/testthat/test-lc_getdata.R | 27 +- tests/testthat/test-lc_nlcd.R | 20 +- tests/testthat/test-sc_get_params.R | 18 +- tests/testthat/test-sc_getdata.R | 6 + tests/testthat/test-sc_nlcd.R | 20 +- vignettes/{ => Articles}/Applications.Rmd | 4 +- vignettes/{ => Articles}/Introduction.Rmd | 23 +- vignettes/{ => Articles}/LakeCat.Rmd | 2 +- vignettes/StartHere.Rmd | 67 + 85 files changed, 20077 insertions(+), 202 deletions(-) create mode 100644 docs/articles/Articles/Applications.html create mode 100644 docs/articles/Articles/Applications_files/HomeButton-0.0.1/LICENSE create mode 100644 docs/articles/Articles/Applications_files/HomeButton-0.0.1/easy-button-src.min.js create mode 100644 docs/articles/Articles/Applications_files/HomeButton-0.0.1/glyphicons-21-home.png create mode 100644 docs/articles/Articles/Applications_files/HomeButton-0.0.1/home-button.js create mode 100644 docs/articles/Articles/Applications_files/Proj4Leaflet-1.0.1/proj4leaflet.js create mode 100644 docs/articles/Articles/Applications_files/basin-CSS-0.0.1/basin_home-button.css create mode 100644 docs/articles/Articles/Applications_files/clipboard-0.0.1/setClipboardText.js create mode 100644 docs/articles/Articles/Applications_files/figure-html/nars_ggplot-1.png create mode 100644 docs/articles/Articles/Applications_files/flowline_nldi___PCTIMP2011CAT-CSS-0.0.1/flowline_nldi___PCTIMP2011CAT_home-button.css create mode 100644 docs/articles/Articles/Applications_files/htmltools-fill-0.5.8.1/fill.css create mode 100644 docs/articles/Articles/Applications_files/htmlwidgets-1.6.4/htmlwidgets.js create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/images/layers-2x.png create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/images/layers.png create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/images/marker-icon-2x.png create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/images/marker-icon.png create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/images/marker-shadow.png create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/leaflet.css create mode 100644 docs/articles/Articles/Applications_files/leaflet-1.3.1/leaflet.js create mode 100644 docs/articles/Articles/Applications_files/leaflet-binding-2.2.3/leaflet.js create mode 100644 docs/articles/Articles/Applications_files/leaflet-providers-2.0.0/leaflet-providers_2.0.0.js create mode 100644 docs/articles/Articles/Applications_files/leaflet-providers-plugin-2.2.3/leaflet-providers-plugin.js create mode 100644 docs/articles/Articles/Applications_files/leafletfix-1.0.0/leafletfix.css create mode 100644 docs/articles/Articles/Applications_files/mapviewCSS-0.0.1/mapview-popup.css create mode 100644 docs/articles/Articles/Applications_files/mapviewCSS-0.0.1/mapview.css create mode 100644 docs/articles/Articles/Applications_files/proj4-2.6.2/proj4.min.js create mode 100644 docs/articles/Articles/Applications_files/rstudio_leaflet-1.3.1/images/1px.png create mode 100644 docs/articles/Articles/Applications_files/rstudio_leaflet-1.3.1/rstudio_leaflet.css create mode 100644 docs/articles/Articles/Introduction.html create mode 100644 docs/articles/Articles/LakeCat.html create mode 100644 docs/articles/StartHere.html create mode 100644 docs/deps/font-awesome-6.5.2/css/all.css create mode 100644 docs/deps/font-awesome-6.5.2/css/all.min.css create mode 100644 docs/deps/font-awesome-6.5.2/css/v4-shims.css create mode 100644 docs/deps/font-awesome-6.5.2/css/v4-shims.min.css create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.ttf create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-brands-400.woff2 create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.ttf create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-regular-400.woff2 create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.ttf create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-solid-900.woff2 create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.ttf create mode 100644 docs/deps/font-awesome-6.5.2/webfonts/fa-v4compatibility.woff2 rename vignettes/{ => Articles}/Applications.Rmd (98%) rename vignettes/{ => Articles}/Introduction.Rmd (89%) rename vignettes/{ => Articles}/LakeCat.Rmd (99%) create mode 100644 vignettes/StartHere.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 93b9297..60c2107 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: StreamCatTools Type: Package Title: 'StreamCatTools' -Version: 0.9.0 +Version: 0.9.1 Authors@R: c(person(given = "Marc", family = "Weber", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index cf71fa4..dfbce09 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,14 @@ +# StreamCatTools 0.9.1 + +- Handles a web service being down in testing using `testthat::skip_on_cran()` +- Converts most vignettes to articles and adds one new `Start Here` vignette + # StreamCatTools 0.9.0 -- Addresses CRAN error generated by tests that is -- Adds tryCatch handling for functions calling web services in order to fail - gracefully and not produce errors -- Adds documentation to vignettes for two new functions added to the package + +- Addresses CRAN error generated by tests that is +- Adds tryCatch handling for functions calling web services in order to fail + gracefully and not produce errors +- Adds documentation to vignettes for two new functions added to the package # StreamCatTools 0.8.0 diff --git a/R/sc_get_params.R b/R/sc_get_params.R index 2941822..afcf51e 100644 --- a/R/sc_get_params.R +++ b/R/sc_get_params.R @@ -179,7 +179,6 @@ sc_get_metric_names <- function(category = NULL, filters <- list(INDICATOR_CATEGORY = category, AOI = aoi, YEAR = year, DSNAME = dataset) - filter_data <- function(data, filters) { # Filter the data frame for each non-null filter filtered_data <- purrr::reduce( @@ -197,14 +196,17 @@ sc_get_metric_names <- function(category = NULL, ) return(filtered_data) } - results <- filter_data(resp, filters) - names_keep <- c("INDICATOR_CATEGORY", "METRIC_NAME", "AOI", "YEAR", - "WEBTOOL_NAME", "METRIC_DESCRIPTION", - "METRIC_UNITS", "SOURCE_NAME", "DSNAME") - results <- results[, names_keep, drop = FALSE] - names_new <- c("Category", "Metric", "AOI", "Year", "Short_Name", - "Metric_Description", "Units", "Source", "Dataset") - names(results) <- names_new + if (exists('resp') & !is.null(resp)){ + results <- filter_data(resp, filters) + names_keep <- c("INDICATOR_CATEGORY", "METRIC_NAME", "AOI", "YEAR", + "WEBTOOL_NAME", "METRIC_DESCRIPTION", + "METRIC_UNITS", "SOURCE_NAME", "DSNAME") + results <- results[, names_keep, drop = FALSE] + names_new <- c("Category", "Metric", "AOI", "Year", "Short_Name", + "Metric_Description", "Units", "Source", "Dataset") + names(results) <- names_new + } else + results <- 'the service appears to be down currently' return(results) } diff --git a/_pkgdown.yml b/_pkgdown.yml index 5af4906..cd7863d 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -5,6 +5,7 @@ articles: - title: Articles navbar: ~ contents: - - Introduction - - LakeCat - - Applications + - StartHere + - Articles/Introduction + - Articles/LakeCat + - Articles/Applications \ No newline at end of file diff --git a/cran-comments.md b/cran-comments.md index 499faf5..a7fb6bb 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,10 +1,14 @@ -This is a minor update that: - -- Addresses CRAN error noted by Dr. Brian Ripley generated by tests that is - required to be fixed by 10/25/25 -- Adds tryCatch handling for functions calling web services in order to fail - gracefully and not produce errors -- Adds documentation to vignettes for two new functions added to the package +This is a patch update that: + +- Handles a web service being down in testing using `testthat::skip_on_cran()` +- Handles a web service being down in vignettes by converting most vignettes to + articles and leaving just one `Start Here` vignette +- Due to the lapse in government funding in federal agencies in the US, + the web service StreamCatTools is built around may be down for some time +- Because of the lapse in government funding in federal agencies in the US, I + may not be able to respond readily to any CRAN emails to my government + email account weber.marc@epa.gov but my personal email is mweber36@gmail.com + ------- @@ -14,19 +18,9 @@ This is a resubmission. ## R CMD check results -Here is the output from `devtools::check()` on R Version 4.4.1, +Here is the output from `devtools::check()` on R Version R version 4.5.0, devtools version 2.4.6, and Windows 11 x64 operating system -0 errors ✔ | 0 warnings ✔ | 2 notes ✖ - -R CMD check succeeded - -NOTES: - -checking installed package size ... NOTE - installed size is 5.0Mb - sub-directories of 1Mb or more: - doc 4.4Mb +Duration: 2m 20.4s -checking for future file timestamps ... NOTE - unable to verify current time +0 errors ✔ | 0 warnings ✔ | 0 notes ✔ diff --git a/docs/404.html b/docs/404.html index f2da380..451d7fb 100644 --- a/docs/404.html +++ b/docs/404.html @@ -13,8 +13,8 @@ - - + + @@ -26,7 +26,7 @@ StreamCatTools - 0.9.0 + 0.9.1 diff --git a/docs/LICENSE.html b/docs/LICENSE.html index ab8e00d..c9e4e34 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -1,5 +1,5 @@ -CC0 1.0 Universal • StreamCatTools +CC0 1.0 Universal • StreamCatTools Skip to contents @@ -7,7 +7,7 @@ StreamCatTools - 0.9.0 + 0.9.1 -