## ############################################################################
##
## The 2024 Lancet Countdown in Europe report
## Indicator 1.3.6: Climatic Suitability for Ticks
##
## Script 06: Create figures
## 
## Authors: 
## - Martín Lotto Batista, ORCID: 0000-0002-9437-5270
## - Prof. Rachel Lowe, ORCID: 0000-0003-3939-7343
##
## Contact: martin.lotto@bsc.es
## 
## ############################################################################

# Packages
pacman::p_load(openxlsx, tidyverse)

# Load the Excel file
wb <- loadWorkbook("data/data_out/suitability_ticks.xlsx")

# Get the names of all sheets in the Excel file
sheet_names <- names(wb)

# Loop through each sheet and save it as an independent object
for (sheet_name in sheet_names) {
  sheet <- read.xlsx(wb, sheet = sheet_name)
  assign(sheet_name, sheet)
}


# Main figure -------------------------------------------------------------
brks <- round(seq(1951, 2022, by = 5),1)
labels <- c("Southern Europe", "Western Europe",
            "Eastern Europe", "Northern Europe",
            "Western Asia")
values <- c("#EE7733", "#009988", "#EE3377", "#33BBEE", "#CC3311")

ylab <- expression(atop("Mean number of months suitable for" ~italic("Ixodes ricinus") ~"activity"))

UN %>% 
  ggplot(aes(x=year, y=Suit, col=UN_region)) +
  geom_line(alpha=0.3, lwd=1) +
  geom_point(size=0.5) +
  geom_smooth(method='lm', formula= y~x, alpha=1,
              se = FALSE, lty=2, lwd=1) +
  scale_color_manual(labels=labels,
                     values=values,
                     guide="legend") +
  theme_classic() +
  labs(x="Year", y=ylab,
       col="") +
  theme(panel.grid.major.y = element_line(colour="grey", linewidth=0.05), 
        axis.text.y=element_text(size=9),
        axis.text.x = element_text(size=9), 
        axis.title.y=element_text(size=10),
        axis.title.x = element_text(size=10), 
        legend.title=element_text(size=9, face="bold"), 
        legend.text=element_text(size=8), 
        plot.title=element_text(size=10, face="bold")) +
  scale_x_continuous(breaks=brks)


# Supplementary figures ---------------------------------------------------
# Rename urbanization categories
urb_rural <- urb_rural %>% 
  mutate(URBN_TYPE=ifelse(URBN_TYPE==1, "Urban", 
                          ifelse(URBN_TYPE==2, "Intermediate", "Rural")))

# Remove missing  values in social deprivation
deprivation <- filter(deprivation, !is.na(Deprvtn))

# Plot time series
plot.ts <- function(dt, group_var, y_var,
                    labels, values, ylab=""){

  brks <- round(seq(1951, 2022, by = 5),1)
  
  dt %>% 
    ggplot(aes(x=year, y={{y_var}}, col={{group_var}})) +
    geom_line(alpha=0.3) +
    geom_point(size=0.5) +
    geom_smooth(method='lm', formula= y~x, alpha=1,
                se = FALSE, lty=2, lwd=0.5) +
    scale_color_manual(labels=labels,
                       values=values,
                       guide="legend") +
    theme_classic() +
    labs(x="Year", 
         y=ylab,
         col="") +
    theme(panel.grid.major.y = element_line(colour="grey", linewidth=0.05), 
          axis.text.y=element_text(size=9),
          axis.text.x = element_text(size=9, angle=35, hjust=0.8), 
          axis.title.y=element_text(size=10),
          axis.title.x = element_text(size=10), 
          legend.position="bottom",
          legend.title=element_text(size=9, face="bold"), 
          legend.text=element_text(size=8), 
          plot.title=element_text(size=10, face="bold")) +
    scale_x_continuous(breaks=brks)
  
}

# UN region
labels <- c("Southern Europe", "Western Europe",
            "Eastern Europe", "Northern Europe",
            "Western Asia")
values <- c("#EE7733", "#009988", "#EE3377", "#33BBEE", "#CC3311")

pt1 <- plot.ts(UN, UN_region, Suit, labels, values) +
  guides(color=guide_legend(nrow=2)) +
  theme(plot.margin=margin(b=0.2, unit="cm"),
        legend.margin=margin(t=-0.2, unit="cm"))

# Income
labels <- c("High-income", "Upper-middle-income")
values <- c("#EE7733", "#009988")

pt2 <- plot.ts(Income, income, Suit, labels, values)

# HDI
labels <- c("High", "Very high")
values <- c("#EE7733", "#009988")

pt3 <- plot.ts(HDI, hdi, Suit, labels, values)

# Urbanization level
labels <- c("Urban", "Intermediate", "Rural")
values <- c("#EE7733", "#009988", "#EE3377")

pt4 <- plot.ts(urb_rural, URBN_TYPE, Suit, labels, values)

# Social deprivation
labels <- c("High", "Medium", "Low")
values <- c("#EE7733", "#009988", "#EE3377")

pt5 <- plot.ts(deprivation, Deprvtn, Suit, labels, values)

# Create an empty plot for arranging the panel
empty_plot <- ggplot() + theme_void()

# Add the y-axis title
ylab <- expression(atop("Mean number of months suitable for" ~italic("Ixodes ricinus") ~"activity"))

plot_axis_title <- ggdraw() +
  draw_label(ylab, size=12, angle=90, x=0.8, y=0.5, hjust=0.5)

# Create panel
above <- cowplot::plot_grid(pt1, pt2, pt3, pt4, ncol=2, 
                         labels=c("a", "b", "c", "d"))

below <- cowplot::plot_grid(empty_plot, pt5, empty_plot, nrow=1, 
                            rel_widths=c(0.45,1,0.45),
                            labels=c("", "e", ""))

right <- cowplot::plot_grid(grid::nullGrob(), above, below, 
                            nrow=3, rel_heights=c(0.02, 1, 0.5))

cowplot::plot_grid(plot_axis_title, right, nrow=1,
                            rel_widths=c(0.05, 0.95))

## END