R/detections/cumulative_individual_station_composition.R

source("R/detections//input_data.R")
source("R/stats_utils.R")
library(gridExtra)


# Print cumulative detection by individuals that make up the key stations station
nodes_to_explore <- c("M08", "M22", "M20", "M21", "G06", "M25")
monthly_detections <- slide_data_by_date_interval(data,
                                                  start,
                                                  end,
                                                  granularity,
                                                  as_list = FALSE)
# --------------------------------------------------
# Cumulative individuals by key stations
# --------------------------------------------------
all_plots <- list()
prev_plot <- NULL
bullet_points <- LETTERS[1:length(nodes_to_explore)]
for (i in seq_along(nodes_to_explore)) {
  station_name <- nodes_to_explore[i]
  df <- data %>%
    dplyr::select(ind_name, station) %>%
    filter(station == station_name) %>%
    group_by(ind_name) %>%
    summarise(detections = n())
  
  num_rows <- nrow(df)
  
  df <- df %>%
    mutate(percentage = round ((detections / sum(detections)) * 100, 2)) %>%
    arrange(desc(percentage)) %>%
    mutate(cum = cumsum(percentage))
  
  df <- df %>% mutate(ind_name = gsub("DICLAB-", "", ind_name))
  main_title <-
    paste0 (str_to_upper(bullet_points[i]),
            ") ",
            station_name)
  g <- generate_cumulative_detection_plot(
    df,
    main_title,
    "Cumulative detections (%)",
    "Individual Detections (%)",
    "ind_name",
    "Individuals",
    "Cumulative Detections (%)",
    show_x_axis_label = ifelse(i > 2, TRUE, FALSE),
    show_legend = FALSE
  )
  
  if (i %% 2 == 1) {
    prev_plot <- g
  }
  else {
    grid_plot <-
      ggarrange(prev_plot,
                g,
                ncol = 2,
                nrow = 1)
    all_plots <- append(all_plots, list(grid_plot))
  }
}

main_common_title <-
  "Most influential individuals at the key stations M08, M22, M20 and M21"
tgrob <- text_grob(main_common_title, size = 15, just = "centre")

grid_title_plot <- ggarrange(plotlist = list (as_ggplot(tgrob)),
                             ncol = 1,
                             nrow = 1)

grid_row1_plot <- ggarrange(all_plots[[1]],
                            ncol = 1,
                            nrow = 1)

grid_row2_plot <- ggarrange(all_plots[[2]],
                            ncol = 1,
                            nrow = 1)

plot_list <- list(grid_title_plot, grid_row1_plot, grid_row2_plot)
outer_grid <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = 3,
    heights = c(1, 5, 5)
  )

to_path <-
  paste(OUTPUT,
        "/",
        "cumulative_individual_station_composition.pdf",
        sep = "")
plots_to_pdf(list(outer_grid),
             to_path,
             paper_type,
             paper_height,
             paper_width)


# --------------------------------------------------
# Cumulative overall individuals and stations
# --------------------------------------------------

# Print cumulative detection by station table
detection_by_stations <- data %>%
  select(station) %>%
  group_by(station) %>%
  summarise(detections = n())  %>%
  mutate(percentage = round ((detections / nrow(data)) * 100, 2)) %>%
  arrange(desc(percentage)) %>%
  head(15) %>%
  mutate(cum = cumsum(percentage))

g_stations <- generate_cumulative_detection_plot(
  detection_by_stations,
  "A) Absolute, partial and cumulative number of detections for stations",
  "Cumulative detections (%)",
  "Station Detections (%)",
  "station",
  "Stations",
  "Cumulative Detections (%)"
)


# Print cumulative detection by individuals table
detection_by_individuals <- data %>%
  select(ind_name) %>%
  group_by(ind_name) %>%
  summarise(detections = n())  %>%
  mutate(percentage = round ((detections / nrow(data)) * 100, 2)) %>%
  arrange(desc(percentage)) %>%
  head(15) %>%
  mutate(cum = cumsum(percentage))

detection_by_individuals <- detection_by_individuals %>%
  mutate(ind_name = gsub("DICLAB-", "", ind_name))

g_individuals <- generate_cumulative_detection_plot(
  detection_by_individuals,
  "B) Absolute, partial and cumulative number of detections for individuals",
  "Cumulative detections (%)",
  "Individual Detections (%)",
  "ind_name",
  "Individuals",
  "Cumulative Detections (%)"
)


main_common_title <-
  "Number of detections for the entire time series for the top 15 stations and individuals"
tgrob <- text_grob(main_common_title, size = 15, just = "centre")

grid_title_plot <- ggarrange(plotlist = list (as_ggplot(tgrob)),
                             ncol = 1,
                             nrow = 1)

grid_title_plot <- ggarrange(plotlist = list (as_ggplot(tgrob)),
                             ncol = 1,
                             nrow = 1)

grid_row1_plot <- ggarrange(g_stations,
                            ncol = 1,
                            nrow = 1)

grid_row2_plot <- ggarrange(g_individuals,
                            ncol = 1,
                            nrow = 1)

plot_list <- list(grid_title_plot, grid_row1_plot, grid_row2_plot)
outer_grid <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = 3,
    heights = c(1, 5, 5)
  )

to_path <-
  paste(OUTPUT,
        "/",
        "overall_cumulative_individual_and_stations.pdf",
        sep = "")
plots_to_pdf(list(outer_grid),
             to_path,
             paper_type,
             paper_height,
             paper_width)


# ----------------------------------------------------------
# Cumulative detections by visiting time to M22
# --------------------------------------------------------

# # Find when M22 is most visited by Medes individuals
m22 <- monthly_detections %>%
  filter(station == "M22") %>%
  select (period, ind_name) %>%
  group_by (period) %>%
  summarise(detections = n()) %>%
  mutate(percentage = round ((detections / sum(detections)) * 100, 2)) %>%
  arrange(desc(percentage)) %>%
  mutate(cum = cumsum(percentage))

m22_plot <- generate_cumulative_detection_plot(
  m22,
  "M22's individual visiting times grouped by months (absolute, partial and cumulative detections)",
  "Cumulative detections (%)",
  "Month Detections (%)",
  "period",
  "Months",
  "Cumulative Detections (%)",
  month_as_name = TRUE
)
to_path <-
  paste(OUTPUT,
        "/",
        "m22_visiting_time_cumulative_detection.pdf",
        sep = "")
plots_to_pdf(list(m22_plot),
             to_path,
             paper_type,
             paper_height,
             paper_width)

# ----------------------------------------------------------
# Cumulative loops
# --------------------------------------------------------

cumulative_loops_totals <- full_metric_details %>%
  select(station, loops) %>%
  group_by (station) %>%
  summarise(detections = sum(loops)) %>%
  mutate(percentage = round ((detections / sum(detections)) * 100, 2))

cumulative_roses <- cumulative_loops_totals %>%
  filter(if_any(station,  ~ str_detect(., "G"))) %>%
  arrange(desc(percentage)) %>%
  mutate(cum = cumsum(percentage))

cumulative_medes <- cumulative_loops_totals %>%
  filter(if_any(station,  ~ str_detect(., "M"))) %>%
  arrange(desc(percentage)) %>%
  mutate(cum = cumsum(percentage))

cumulative_loops_totals <- cumulative_loops_totals %>%
  arrange(desc(percentage)) %>%
  mutate(cum = cumsum(percentage)) %>% 
  head(30)

cum_loops_roses_plots <- generate_cumulative_detection_plot(
  cumulative_roses,
  "Badia de Roses",
  "Cumulative detections (%)",
  "Individual Loops (%)",
  "station",
  "Stations",
  "Individual Loops (%)",
  show_x_axis_label = FALSE,
  show_legend = FALSE
)


cum_loops_medes_plots <- generate_cumulative_detection_plot(
  cumulative_medes,
  "Illes Medes",
  "Cumulative detections (%)",
  "Individual Loops (%)",
  "station",
  "Stations",
  "Individual Loops (%)",
  show_x_axis_label = FALSE,
  show_legend = FALSE
)

cum_loops_total_plots <- generate_cumulative_detection_plot(
  cumulative_loops_totals,
  "All zones(30 top stations)",
  "Cumulative detections (%)",
  "Individual Loops (%)",
  "station",
  "Stations",
  "Individual Loops (%)",
  show_x_axis_label = TRUE,
  show_legend = FALSE
)

main_common_title <-
  "Number of loops at Illess Medes, Badia de Roses and all zones for the entire series"
tgrob <- text_grob(main_common_title, size = 15, just = "centre")


grid_title_plot <- ggarrange(plotlist = list (as_ggplot(tgrob)),
                             ncol = 1,
                             nrow = 1)

grid_row1_plot <- ggarrange(cum_loops_medes_plots,
                            cum_loops_roses_plots,
                            ncol = 1,
                            nrow = 2)

grid_row2_plot <- ggarrange(cum_loops_total_plots,
                            ncol = 1,
                            nrow = 1)

plot_list <- list(grid_title_plot, grid_row1_plot, grid_row2_plot)
outer_grid <-
  ggarrange(
    plotlist = plot_list,
    ncol = 1,
    nrow = 3,
    heights = c(1, 10, 5)
  )

to_path <-
  paste(OUTPUT,
        "/",
        "loops_zones_overall_cumulative.pdf",
        sep = "")
plots_to_pdf(list(outer_grid),
             to_path,
             paper_type,
             paper_height,
             paper_width)
d2gex/seabasstfm documentation built on July 29, 2022, 2:20 a.m.