### Adults captures on traps (2023)
## After changing the name of the variables, we used the same code to plot other captures 
## and the insects abundance in the surrounding vegetation.

rm(list = ls())
library(ggplot2)
library(dplyr)

path <- "C:/path_to_the_data/file.csv"
df<-read.csv(path)
df$HH.adult <- as.numeric(df$HH.adult)
str(df)

## Data organisation

mean_df <- aggregate(HH.adult ~ name + Week, df, FUN = mean, na.rm = TRUE)
sd_df <- aggregate(HH.adult ~ name + Week, df, FUN = sd, na.rm = TRUE)
df1 <- merge(mean_df, sd_df, by = c("name", "Week"))
colnames(df1)[3:4] <- c("HH.adult_mean", "HH.adult_sd")
count_df <- aggregate(HH.adult ~ name + Week, df, FUN = function(x) sum(!is.na(x)))
df1 <- merge(df1, count_df, by = c("name", "Week"))
colnames(df1)[5] <- "count"
df1$HH.adult_se <- df1$HH.adult_sd / sqrt(df1$count)
df1$Week <- as.Date(df1$Week, format = "%d/%m/%Y")
df1$Week <- gsub("-", "/", df1$Week)
df1 <- df1 %>% arrange(Week)
df1$Week <- as.Date(df1$Week, format = "%Y/%m/%d")
unique(df1$Week)
df1$name <- factor(df1$name, levels = c(setdiff(unique(df1$name), "CNT"), "CNT"))

## Plotting the data

graph<-ggplot(data = df1, aes(x = Week, y = HH.adult_mean, color = name, group = name)) +
  geom_area(color = 'black', fill = 'lightgray', alpha = 0.4) +  
  geom_point(size = 1.5, shape = "x", color = 'red') +
  geom_errorbar(aes(ymin = HH.adult_mean - HH.adult_se, ymax = HH.adult_mean + HH.adult_se), 
                width = 1.5, linewidth= 0.2,color = "black")+
  labs( x = "Week", y = "Adult captures (mean ± SE)", ) +   
  facet_wrap(~ name, scales = "free_x") +  
  scale_x_date(
    date_labels = "%d-%b", 
    breaks = seq(min(df1$Week), max(df1$Week), by = "3 week"), 
    expand = expansion(mult = c(0.05, 0.05))  
  ) +
  scale_y_continuous(limits = c(min(df1$HH.adult_mean - df1$HH.adult_se), 
                                max(df1$HH.adult_mean + df1$HH.adult_se)), 
                     breaks = seq(0, 12, by = 3)) +  
  theme_bw(base_rect_size = 1) +  
  theme(
    panel.background = element_blank(), 
    panel.grid.major = element_blank(),  
    panel.grid.minor = element_blank(),
    legend.position = "none",  
    axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.3, size = 8, color = "black"), 
    axis.text.y = element_text(hjust = 0.5, vjust = 0.3, size = 8, color = "black"),
    strip.text = element_text(hjust = 0.5, size = 10, color = "black"),  
    panel.spacing = unit(1, "lines"),
    axis.title.y = element_text(size = 12, color="black"),
    axis.title.x = element_text(size = 12, color="black"),
    axis.ticks.x = element_line(color = "black"),
    axis.ticks.y = element_line(color = "black"),
    panel.border = element_rect(
      color = "black",  
      fill = NA,  
      size = 1,  
      linetype = "solid"
    ),
    strip.background = element_rect(
      color = "black",  
      fill = "grey",  
      size = 0.5,  
      linetype = "solid"
    )
  )

## Visualisation

plot(graph)

## Saving the image

ggsave("name_of_the_image.png", plot = graph, dpi = 1200, width = 12, height = 9, units = "in")



### Adults captures in traps (2024)
## After changing the name of the variables, we used the same code to plot other captures 
## and the insects abundance in the surrounding vegetation.

rm(list = ls())
library(ggplot2)
library(dplyr)

path <- "C:/path_to_the_data/file.csv"
df<-read.csv(path)
df$HH.adult<-(df[,5]+df[,6])
df$HH.adult <- as.numeric(df$HH.adult)
str(df)

## Data organisation

mean_df <- aggregate(HH.adult ~ name + Week, df, FUN = mean, na.rm = TRUE)
sd_df <- aggregate(HH.adult ~ name + Week, df, FUN = sd, na.rm = TRUE)
df1 <- merge(mean_df, sd_df, by = c("name", "Week"))
colnames(df1)[3:4] <- c("HH.adult_mean", "HH.adult_sd")
count_df <- aggregate(HH.adult ~ name + Week, df, FUN = function(x) sum(!is.na(x)))
df1 <- merge(df1, count_df, by = c("name", "Week"))
colnames(df1)[5] <- "count"
table(df1$count)
summary(df$HH.adult)
sum(is.na(df$HH.adult))  
df1$HH.adult_se <- df1$HH.adult_sd / sqrt(df1$count)
df1$Week <- as.Date(df1$Week, format = "%d/%m/%Y")
df1$Week <- gsub("-", "/", df1$Week)
df1 <- df1 %>% arrange(Week)
df1$Week <- as.Date(df1$Week, format = "%Y/%m/%d")
unique(df1$Week)
df1$name <- factor(df1$name, levels = c(setdiff(unique(df1$name), "CNT"), "CNT"))

graph_order <- c(
  "BLS_SL_00", "BLS_SL_MO", "BLS_SL_FI", "BLS_SL_FM", 
  "BLS_DL_00", "BLS_DL_MO", "BLS_DL_FI", "BLS_DL_FM", 
  "BIP_SL_00", "BIP_SL_FM", "BIP_SL_ML", "BIP_DL_00", 
  "BIP_DL_FM", "BIP_DL_ML", "BIP_DL_MLM", 
  "CNT"
)
df1$name <- factor(df1$name, levels = graph_order)

## Plotting the data

graph<-ggplot(data = df1, aes(x = Week, y = HH.adult_mean, color = name, group = name)) +
  geom_area(color = 'black', fill = 'lightgray', alpha = 0.4) +  
  geom_point(size = 1.5, shape = "x", color = 'red') +
  geom_errorbar(aes(ymin = HH.adult_mean - HH.adult_se, ymax = HH.adult_mean + HH.adult_se), 
                width = 1, linewidth= 0.2,color = "black")+
  labs( x = "Week", y = "Adult captures (mean ± SE)", ) +  
  facet_wrap(~ name, scales = "free_x") +  
  scale_x_date(
    date_labels = "%d-%b", 
    breaks = seq(min(df1$Week), max(df1$Week), by = "3 week"), 
    expand = expansion(mult = c(0.05, 0.05))  
  ) +
  scale_y_continuous(limits = c(min(df1$HH.adult_mean - df1$HH.adult_se), 
                                max(df1$HH.adult_mean + df1$HH.adult_se)), 
                     breaks = seq(0, 12, by = 3)) +  
  theme_bw(base_rect_size = 1) +  
  theme(
    panel.background = element_blank(), 
    panel.grid.major = element_blank(),  
    panel.grid.minor = element_blank(),
    legend.position = "none",  
    axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.3, size = 8, color = "black"), 
    axis.text.y = element_text(hjust = 0.5, vjust = 0.3, size = 8, color = "black"),
    strip.text = element_text(hjust = 0.5, size = 10, color = "black"),  
    panel.spacing = unit(1, "lines"),
    axis.title.y = element_text(size = 12,color = "black"),
    axis.title.x = element_text(size = 12,color = "black"),
    axis.ticks.x = element_line(color = "black"),
    axis.ticks.y = element_line(color = "black"),
    panel.border = element_rect(
      color = "black",  
      fill = NA,
      size = 1, 
      linetype = "solid"
    ),
    strip.background = element_rect(
      color = "black",
      fill = "grey",  
      size = 0.5,  
      linetype = "solid"
    )
  )

## Visualisation

plot(graph)

## Saving the image

ggsave("name_of_the_image.png", plot = graph, dpi = 1200, width = 12, height = 9, units = "in")

