From 101e28492bd171968edddfef34304ba62d58cbb1 Mon Sep 17 00:00:00 2001 From: Platt Date: Mon, 22 Aug 2022 15:08:44 -0500 Subject: [PATCH] change water temp vals to rolling 10 day averages --- .../2_process/src/clean_param_data.R | 25 ++++++++++++++++--- .../src/generate_timeseries_figure.R | 4 +-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/data-pipeline/2_process/src/clean_param_data.R b/data-pipeline/2_process/src/clean_param_data.R index 19546f4..0332bca 100644 --- a/data-pipeline/2_process/src/clean_param_data.R +++ b/data-pipeline/2_process/src/clean_param_data.R @@ -1,6 +1,23 @@ clean_param_data <- function(file_in, sites, start_date, end_date) { - readr::read_csv(file_in, show_col_types = FALSE) %>% - filter(site_no %in% sites) %>% - filter(date >= start_date, date <= end_date) %>% - select(site_no, date, param_grp = parm_abbrev, ends_with("_result")) + data_clean <- readr::read_csv(file_in, show_col_types = FALSE) %>% + dplyr::filter(site_no %in% sites) %>% + dplyr::mutate(param_grp = parm_abbrev) %>% + dplyr::arrange(site_no, date) %>% + # Adjust temperature values to be a rolling 10-day `mean_result` values + # before the date filtering so that we include dates outside of the window + convert_temps_to_rolling_mean() %>% + dplyr::filter(date >= start_date, date <= end_date) %>% + dplyr::select(site_no, date, param_grp, ends_with("_result"), plot_value) + +} + +convert_temps_to_rolling_mean <- function(data_in) { + data_in %>% + dplyr::group_by(site_no, param_grp) %>% + # Calculate the rolling mean per site/param_grp + dplyr::mutate(mean_rolling = zoo::rollmean(mean_result, k = 10, fill=NA, align = "center")) %>% + # Only replace `mean_result` with the rolling mean for temperature + dplyr::mutate(plot_value = ifelse(param_grp == "temp_water", mean_rolling, mean_result)) %>% + dplyr::select(-mean_rolling) %>% + dplyr::ungroup() } diff --git a/data-pipeline/3_visualize/src/generate_timeseries_figure.R b/data-pipeline/3_visualize/src/generate_timeseries_figure.R index 398bb4a..829697d 100644 --- a/data-pipeline/3_visualize/src/generate_timeseries_figure.R +++ b/data-pipeline/3_visualize/src/generate_timeseries_figure.R @@ -16,13 +16,13 @@ generate_line_ts_figure <- function(file_out, data_in, plot_label = 'Mean daily mutate(alpha_val = ifelse(alpha_val > 1, 2 - alpha_val, alpha_val)) p <- ggplot(data = data_in) + - geom_text(data = data_last, aes(x = date, y = mean_result, label = site_label, color = site_label), + geom_text(data = data_last, aes(x = date, y = plot_value, label = site_label, color = site_label), fontface ="bold", hjust = 0, nudge_x = 0.5) + geom_text(x = min(data_in$date), y = 107, label = plot_label, color = '#918b8b', size = 5, hjust = 0.10) + geom_rect(data = gradient_rect, aes(xmin = xleft, xmax = xright, alpha = alpha_val), ymin = -Inf, ymax = Inf, fill = '#dedcdc') + - ggalt::geom_xspline(aes(x = date, y = mean_result, color = site_label), size = 1, spline_shape = -0.5) + + ggalt::geom_xspline(aes(x = date, y = plot_value, color = site_label), size = 1, spline_shape = -0.5) + scale_color_manual(values = color_vals, breaks = color_val_labels) + theme_bw() +