rossyndicate / poudre_sonde_network

MIT License
0 stars 7 forks source link

Fix generate_daily_flag_plots() #55

Closed juandlt-csu closed 11 months ago

juandlt-csu commented 11 months ago

After changing the criteria for add_slope_flags this is not displaying the expected information. Need to explore and resolve this.

kathryn-willi commented 11 months ago

Working code, where all old monthly summaries are swapped for seasonal summaries, and I've also filtered to 2023 to find a good example of the code working:

# Visualizing daily plots

## This function will generate a list of plots for site-parameters
## that have been tagged by a specific flag 
generate_daily_flag_plots <- function(site_arg, parameter_arg, flag_arg = NULL) {

  # Generating df name to pull from all_data_flagged list
  site_param <- paste0(site_arg, "-", parameter_arg)
  # filter for all the days that are tagged within the site-param df of interest
  site_flag_dates <- all_data_flagged[[site_param]] %>%
        filter(year == "2023") %>%
      filter(if (is.null(flag_arg)) !is.na(flag) else str_detect(flag, flag_arg)) %>%
      group_by(day(DT_join)) %>%
      slice(1)

  # for loop to generate plots for everyday that was tagged by a flag
  plot_list <- list()

  for (i in 1:nrow(site_flag_dates)) {

    flag_title <- site_flag_dates$flag[i]
    flag_year <- site_flag_dates$year[i]
    flag_month <- site_flag_dates$month[i]
    flag_day <- site_flag_dates$DT_round[i]

    plot_data <- all_data_flagged[[site_param]] %>%
      filter(year == "2023") %>%
      filter(year == flag_year,
             month == flag_month,
             day(DT_round) == day(flag_day))

    plot <- ggplot(data = plot_data) +
      geom_point(aes(x=DT_round, y = mean, color = flag)) +
      # exceeding sd visualized
      geom_line(aes(x = DT_round, y = rollavg, color = "mean"), show.legend = TRUE) +
      geom_ribbon(aes(ymin = rollavg - m_sd, ymax = rollavg + m_sd, x = DT_round), alpha = 0.1, color = NA) +
      geom_ribbon(aes(ymin = rollavg - (m_sd*2), ymax = rollavg + (m_sd*2), x = DT_round), alpha = 0.1, color = NA) +
      geom_ribbon(aes(ymin = rollavg - (m_sd*3), ymax = rollavg + (m_sd*3), x = DT_round), alpha = 0.1, color = NA) +
      # exceeding slope visualized
      geom_vline(data = (plot_data %>% filter(is.na(mean))), aes(xintercept = DT_round, color = flag)) +
      theme_bw() +
      theme(legend.position = 'bottom') +
      ggtitle(paste(flag_title ,"at", site_arg, "on", as.character(flag_day))) + # HERE
      labs(x = "Datetime",
           y = "Mean")

    plot_list[[paste(site_param, as.character(flag_day))]] <- plot

  }

  return(plot_list)

}

generate_daily_flag_plots("archery", "Temperature", "slope flag suspect")[[9]]
juandlt-csu commented 11 months ago

I fixed this issue by grouping by year as well as by day in my code