library(tidyverse)
library(dplyr)
library(ggplot2)
library(ggpubr)
#library(reshape2)
#library(hrbrthemes)
library("gridExtra")
#library(cowplot)
library(plotly)
library(zoo)
knitr::opts_chunk$set(echo = TRUE)
data <- read.csv("/Users/nuriteliash/Documents/GitHub/Hive-monitoring/data/monitor_ayelet_2023.csv")

df = data %>% 
  dplyr::mutate(fall_norm = fall/falling_time) %>%
  dplyr::mutate(date = as.Date(date, format = "%d/%m/%Y")) %>% # Convert to Date object 
  dplyr::mutate(sugar = as.numeric(sugar)) %>%
  dplyr::mutate(brood = as.numeric(brood)) 

# order the hive number:
df$hive <- factor(df$hive, level=c("hive1" ,"hive2","hive3","hive4","hive5","hive6","hive7","hive8","hive9","hive10","hive11" ,"hive12","hive13","hive14","hive15","hive16","hive17","hive18","hive19","hive20"))

Mite infestation level

mite infestation level - free fall

df %>% select(c("date", "hive","fall_norm")) %>%
  #dplyr::filter(hive != "hive18") %>%
   dplyr::filter(date != "2023-06-07") %>% # i removed this date. as it has high counts, and i dont understand why
 na.omit() %>% 
  ggplot(aes(x = date, y = fall_norm, group = hive, color = hive)) +         
  geom_line() + 
  geom_point() +
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
  annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
  annotate("text", x = as.Date("2023-08-15"), y = max(df$fall_norm, na.rm = TRUE), label = "Summer harvest", color = "blue",vjust = 5, hjust=-0.05) +
  # scale_x_date(date_labels = "%d/%m", date_breaks = "1 month") +
  ylab("Mite count (falling/day)") +
  labs(title = "Varroa infestation level",
       subtitle = "June-October 2023, North district, hot Mediterranean climate") +
  theme_classic() +
  theme(legend.position = "none")

 df %>% select(c("date", "hive","fall_norm")) %>%
 # dplyr::filter(hive != "hive18") %>%
     dplyr::filter(date != "2023-06-07") %>%
na.omit() %>%
  ggplot(aes(x = date, y = fall_norm, group = hive, color = hive)) +         
  geom_line() + 
  geom_point() +
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
 # annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
 geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
scale_x_date(date_labels = "%d/%m", date_breaks = "1 month") +
  ylab("Mite count (falling/day)") +
  labs(title = "Varroa infestation level",
       subtitle = "June-October 2023, North district, hot Mediterranean climate") +
  theme_classic() +
  theme(legend.position = "none")+
   facet_wrap(~hive)+
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust=0.5)) +
     xlab(element_blank()) 

ggplot(df, aes(x=date, y=fall_norm)) +
  geom_point() +
    geom_jitter() +
  ylab("Mite count (falling/day)") +
  geom_smooth(method="loess" , color="red",  se=TRUE, level=0.99)+
     theme_classic()+geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
  annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
  annotate("text", x = as.Date("2023-08-15"), y = max(df$fall_norm, na.rm = TRUE), label = "Summer harvest", color = "blue",vjust = 5, hjust=-0.05)

mite infestation level - powder sugar roll

p_all_sugar = df %>% select(c("date", "hive","sugar")) %>%
    na.omit() %>%
ggplot(aes(x = date, y = sugar, group=hive, color=hive)) +         
  geom_line() + 
      geom_point() +
scale_x_date(date_labels = "%d/%m", date_breaks = "2 weeks")+
  ylab("Mite count, sugar roll") +
    labs(title = "Mite count-sugar roll") +
    theme_classic() +
  # facet_wrap(~hive)+
     theme(legend.position = "none") +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust=0.5)) 

ggplotly(p_all_sugar)
p_facet_sugar = df %>% select(c("date", "hive","sugar")) %>%
    na.omit() %>%
ggplot(aes(x = date, y = sugar, group=hive, color=hive)) +         
  geom_line() + 
      geom_point() +
scale_x_date(date_labels = "%d/%m", date_breaks = "1 month")+
  ylab("Mite count, sugar roll") +
    labs(title = "Mite count-sugar roll") +
    theme_classic() +
  facet_wrap(~hive)+
     theme(legend.position = "none") +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust=0.5)) +
     xlab(element_blank()) 

ggplotly(p_facet_sugar)
ggplot(df, aes(x=date, y=sugar)) +
  geom_point() +
    ylab("Mite count (sugar roll)") +
  geom_smooth(method="loess" , color="red",  se=TRUE, level=0.99)+
     theme_classic()+
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
  annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
  annotate("text", x = as.Date("2023-08-15"), y = max(df$fall_norm, na.rm = TRUE), label = "Summer harvest", color = "blue",vjust = 5, hjust=-0.05)

Bee population

Bee population - brood frames

df %>% select(c("date", "hive","brood")) %>% 
   na.omit() %>% 
  ggplot(aes(x = date, y = brood, group = hive, color = hive)) +         
  geom_line() + 
  geom_point() +
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
  annotate("text", x = as.Date("2023-08-02"), y = max(df$brood, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
  annotate("text", x = as.Date("2023-08-15"), y = max(df$brood, na.rm = TRUE), label = "Summer harvest", color = "blue",vjust = 5, hjust=-0.05) +
  # scale_x_date(date_labels = "%d/%m", date_breaks = "1 month") +
  ylab("Honey bee brood frames") +
  labs(title = "Honey bee brood",
       subtitle = "June-October 2023, North district, hot Mediterranean climate") +
  theme_classic() +
  theme(legend.position = "none")

 df %>% select(c("date", "hive","brood")) %>%
 # dplyr::filter(hive != "hive18") %>%
     dplyr::filter(date != "2023-06-07") %>%
na.omit() %>%
  ggplot(aes(x = date, y = brood, group = hive, color = hive)) +         
  geom_line() + 
  geom_point() +
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
 # annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
   geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
  scale_x_date(date_labels = "%d/%m", date_breaks = "1 month") +
  ylab("Honey bee brood frames") +
 labs(title = "Honey bee brood",
       subtitle = "June-October 2023, North district, hot Mediterranean climate") +
  theme_classic() +
  theme(legend.position = "none")+
   facet_wrap(~hive)+
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust=0.5)) +
     xlab(element_blank()) 

ggplot(df, aes(x=date, y=brood)) +
  geom_point() +
  geom_jitter() +
  ylab("Honey bee brood frames") +
  geom_smooth(method="loess", color="red", se=TRUE, level=0.99) +
  theme_classic() +
  geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
  annotate("text", x = as.Date("2023-08-02"), y = max(df$brood, na.rm = TRUE) * 1.1, label = "Amitraz treatment", color = "red", vjust = 0.5, hjust=-0.05) +
  geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # Summer harvest
  annotate("text", x = as.Date("2023-08-15"), y = max(df$brood, na.rm = TRUE) * 1.1, label = "Summer harvest", color = "blue", vjust = 5, hjust=-0.05) +
  ylim(min(df$brood, na.rm = TRUE), max(df$brood, na.rm = TRUE) * 1.2)

Bee population - populated frames

df %>% select(c("date", "hive","populated")) %>%
    na.omit() %>%
  ggplot(aes(x = date, y = populated, group=hive, color=hive)) +         
  geom_line() +
    geom_point() +
scale_x_date(date_labels = "%d/%m", date_breaks = "1 month")+
  ylab("Honeybee populated frames") +
    labs(title = "Honeybee populated frames") +
    theme_classic()  +
  facet_wrap(~hive)+
     theme(legend.position = "none") 

ggplot(df, aes(x=date, y=populated)) +
  geom_point() +
  ylab("Honeybee populated frames") +
  geom_smooth(method="loess" , color="red",  se=TRUE, level=0.99)+
     theme_classic()

  #geom_vline(xintercept = as.Date("2023-08-02"), linetype = "dashed", color = "red") +  # Amitraz treatment
 # annotate("text", x = as.Date("2023-08-02"), y = max(df$fall_norm, na.rm = TRUE), label = "Amitraz treatment", color = "red",vjust = 0.5, hjust=-0.05) +
#geom_vline(xintercept = as.Date("2023-08-15"), linetype = "dashed", color = "blue") +  # summer harvest
 # annotate("text", x = as.Date("2023-08-15"), y = max(df$fall_norm, na.rm = TRUE), label = "Summer harvest", color = "blue",vjust = 5, hjust=-0.05)

Bee-mite population

df %>%
  select(c("date", "hive", "brood", "fall_norm")) %>%
  group_by(hive) %>%
  mutate(
    brood_filled = na.fill(brood, "extend"),     # Temporarily fill NAs to connect lines
    fall_norm_filled = na.fill(fall_norm, "extend") / 10  # Temporarily fill and scale fall_norm
  ) %>%
  ggplot(aes(x = date, group = hive)) +         
  geom_line(aes(y = brood_filled), color = "blue") +  # Use filled data to keep lines connected
  geom_point(aes(y = brood), color = "blue") +  # Plot original data with NAs
  geom_line(aes(y = fall_norm_filled), color = "red") +  # Use filled data to keep lines connected
  geom_point(aes(y = fall_norm / 10), color = "red") +  # Plot original data with NAs
  scale_x_date(date_labels = "%d/%m", date_breaks = "1 month") +
  ylab("Honeybee brood combs") +
 labs(title = "Honey bee and mite populations along the season") +
  theme_classic() +
  scale_y_continuous(
    name = "Honey bee brood comb", 
    sec.axis = sec_axis(~.*10, name = "Mite count, free falling/day")) + 
  theme(
    axis.title.y = element_text(size = 13, color = "blue"),
    axis.title.y.right = element_text(size = 13, color = "red")) +
  facet_wrap(~hive) +
  theme(legend.position = "none") +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust = 0.5)) +
  xlab(element_blank())

Correlations:

Sugar roll and free-fall:

df %>% 
ggplot(aes(x = sugar, y = fall_norm)) +         
  geom_point() + 
  ylab("Mite count, normalized free falling") +
  xlab("Mite count, sugar roll") +
  labs(title = "Correlation between mite count in free falling (normalized)
and mite count in sugar roll") +
    theme_classic()+ geom_smooth(method=lm) +
  stat_cor(label.x = 3, label.y = 45) +
  stat_regline_equation(label.x = 3, label.y = 40) +
  ylim(0, 50)