R/graph_metrics/tfm_graphs/individuals_and_detection_details.R

source("R/graph_metrics/input_data.R")
source("R/graph_metrics/grid_plotting_metric_utils.R")
source("R/stats_utils.R")
source("R/detections/detection_views.R")
library(grid)

fish_extra_data <-
  read.csv("data/res_med/2020_2022/resmed_fish_database.csv")
av_path_length <- get_apl_monthly_metrics(data)
monthly_detections <- slide_data_by_date_interval(data,
                                                  start,
                                                  end,
                                                  granularity,
                                                  as_list = FALSE)

detections <- build_all_detection_matrices(monthly_detections,
                                           ind_or_station = TRUE)

max_indiviuals <- 10

# Build data matrix to plot
ind_det_av_path_length <-
  merge(av_path_length, detections$all, all = TRUE)

ind_det_av_path_length <- ind_det_av_path_length %>%
  select(-period) %>%
  get_all_metrics_sum_by_group_name ('ind_name') %>%
  mutate(ind_name = gsub("DICLAB-", "", ind_name))


# Generate cumulative plot for individual detections
detection_by_individuals <- ind_det_av_path_length %>%
  select(ind_name, detections) %>% 
  mutate(percentage = round ((detections / sum(detections)) * 100, 2)) %>%
  arrange(desc(percentage)) %>%
  head(max_indiviuals) %>%
  mutate(cum = cumsum(percentage))

det_individual_plot <- generate_cumulative_detection_plot(
  detection_by_individuals,
  "B) Detections",
  NULL,
  NULL,
  "ind_name",
  "Individuals",
  "Cumulative Detections (%)",
  show_x_axis_label = FALSE,
  show_legend = FALSE
)

# Generate cumulative plot for individual average path length

av_by_individuals <- ind_det_av_path_length %>%
  select(ind_name, mean_distance) %>% 
  rename(detections = mean_distance) %>% 
  mutate(percentage = round ((detections / sum(detections)) * 100, 2)) %>%
  arrange(desc(percentage)) %>%
  head(max_indiviuals) %>%
  mutate(cum = cumsum(percentage))

av_individual_plot <- generate_cumulative_detection_plot(
  av_by_individuals ,
  "B) Average Path Length",
  NULL,
  NULL,
  "ind_name",
  "Individuals",
  "Cumulative Average path Length (%)",,
  show_legend = FALSE
)

# Generate table of individual names, zones and lengths

selected_individuals <- av_by_individuals$ind_name
individual_order <- max_indiviuals:1
df_indiv_order <- data.frame(ind_name=selected_individuals, 
                             ind_order =individual_order)

extra_data <- fish_extra_data %>%
  rename(ind_name = "ID.Code") %>%
  filter(Species == "Dicentrarchus labrax") %>%
  select(ind_name, Length, Zone) %>%
  mutate(ind_name = paste0("DICLAB-", ind_name)) %>% 
  select(ind_name, Length, Zone) %>% 
  mutate(Zone=str_replace_all(Zone, "[^a-zA-Z0-9\\s]", "")) %>% 
  mutate(ind_name = gsub("DICLAB-", "", ind_name)) %>% 
  filter(ind_name %in% selected_individuals) %>% 
  merge(df_indiv_order, all=TRUE) %>% 
  arrange(desc(ind_order)) %>% 
  select(-ind_order)

extra_data <- extra_data[, c("ind_name", "Zone", "Length")]
names(extra_data) <- c("Individuals", "Fishing Location", "Total Length")
ind_details <- tableGrob(extra_data, rows = NULL, theme = tt3)

  
main_common_title <-
  "Number of detections and average path length for the 10 top 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_row1_plot <- ggarrange(det_individual_plot,
                            ncol = 1,
                            nrow = 1)

grid_row2_plot <- ggarrange(av_individual_plot,
                            ind_details,
                            ncol = 2,
                            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_detection.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.