############################################################################
## Purpose: Figure for Lancet Regional Health Europe Comment entitled "Approaching unsafe limits: climate-related health inequities"
## Title: Monthly mean temperature anomalies in Europe between 1950 an 2023
## Code adapted from Prof. Pat Schloss climate spiral R code: https://github.com/riffomonas/climate_viz/commit/fc62e9ea121994641d0fee1c7369768631dadbda
##
## Author: Kim Robin van Daalen
## Contact information: kim.vandaalen@bsc.es
## Date of creation: 20 June 2023 
##
## Input data: 
## - Monthly 2m mean temperature ERA5 
## - Period: January 1950 to May 2023 
## - Coordinates Europe: 10W-40E, 30-75N
##
############################################################################

## load packages 
library(readxl)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(ggrepel)

## load data 
temp <- read_csv("data/2mt_era5_Europe_195001-202305.csv") %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(date = format(date,"%d %b %Y")) %>% 
  separate(date, c("day", "month", "year"), " ") %>%
  select(meant,month,year) %>% 
  mutate(year = as.numeric(year))

## generate 1979-2000 baseline for temperature anomalies 
baseline <- temp %>% 
  filter(year > 1978 & year < 2001) %>% 
  group_by(month) %>% 
  summarise_at(vars(meant), list(base_tem = mean)) 

## calculate temperature anomalies from 1979-2000 baseline 
t_diff <- merge(temp, baseline, by = 'month') %>% 
  mutate(tem_dif = meant-base_tem) %>% 
  select(month,year,tem_dif,base_tem) 

## ensure that the lines go from dec to jan next year within the figure
next_jan <- t_diff %>%
  filter(month == "Jan") %>%
  mutate(year = year - 1,
         month = "next_Jan")

## add next_jen to data table 
t_diff2 <- bind_rows(t_diff, next_jan) %>%
  mutate(month = factor(month, levels = c(month.abb, "next_Jan")),
         month_number = as.numeric(month))

## mark final month within figure
annotation <- t_diff2 %>%
  slice_max(year) %>% 
  slice_max(month_number)

## add temperature lines within figure 
temp_lines <- tibble(
  x = 12,
  y = c(-1,0,1,2,3),
  labels = c("-1\u00B0C","0\u00B0C","1\u00B0C", "2\u00B0C","3\u00B0C")
)

## add month labels within figure  
month_labels <- tibble(
  x = 1:12,
  labels = month.abb,
  y = 3.6
)

## highlight 2023 with a different color within figure
now <- t_diff2 %>% 
  filter(year == 2023)

## add a label for last 2023 point within figure
now_anno <- annotation %>% 
  filter(year == 2023)

## create figure 
plot <- t_diff2 %>% 
  ggplot(aes(x = month_number, y = tem_dif, group = year, color = year)) +
  geom_col(data = month_labels, aes(x = x + 0.5, y = 2.4), fill = "white",
           width = 1,
           inherit.aes = FALSE) +
  geom_col(data = month_labels, aes(x = x + 0.5, y = -2), fill = "white",
           width = 1,
           inherit.aes = FALSE) +
  geom_hline(yintercept = c(-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3), color = "#E5E5E5") +
  geom_line(size = 0.5) +
  geom_line(data = now, col = "#B5338A") +
  geom_hline(yintercept = 0, color = "green", size = 0.6)+
  geom_text_repel(aes(label = year), data = now_anno, col = "#B5338A", nudge_x = -0.1, nudge_y = 0.5) +
  geom_point(data = annotation, aes(x = month_number, y = tem_dif, color = year),
             size = 2,
             inherit.aes = FALSE) +
  geom_point(data = now_anno, aes(x = month_number, y = tem_dif, color = year),
             size = 2, col = "#B5338A",
             inherit.aes = FALSE) +
  geom_label(data = temp_lines, aes(x = x, y = y, label = labels),
             color = "black", fill = "#72bcd4", size = 3.4, label.size = 0.2,
             inherit.aes = FALSE) +
  geom_text(data = month_labels, aes(x = x, y = y, label = labels, parse = TRUE),
            inherit.aes = FALSE, color = "#4682B4", 
            angle = seq(360 - 360/12, 0, length.out = 12)) +
  scale_x_continuous(breaks = 1:12,
                     labels = month.abb, expand = c(0,0),
                     sec.axis = dup_axis(name = NULL, labels=NULL)) +
  scale_y_continuous(breaks = seq(-4, 4, 1),
                     limits = c(-4, 4), 
                     sec.axis = dup_axis(name = NULL, labels = NULL)) +
  scale_color_viridis_c(option = "G",breaks = seq(1950, 2023, 20)) +
  coord_cartesian(xlim = c(1,12)) +
  coord_polar(start = 2*pi/12) +
  labs(x = NULL,
       y = NULL,
       title = "Monthly mean 2m temperature anomalies in Europe (10°W-40°E, 30-75°N) \n 1950-2023 (compared to a 1979-2000 baseline)", 
       color = "Year") +
  theme(
    panel.background = element_rect(fill = "white", size = 1),
    plot.background = element_rect(fill = "white", color = "white"),
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_text(color = "black", size = 13),
    plot.title = element_text(color = "black", hjust = 0.5, vjust = -20, size = 14, face = "bold"),
    legend.title = element_text(color = "black", size = 8.5, vjust = 0.8, face = "bold"),
    legend.position = c(0.5, 0.065), 
    legend.direction = "horizontal", 
    legend.key.size = unit(1.4, "lines"), 
    legend.background = element_rect(fill = "white"), 
    legend.key.width = unit(1.5,"cm") 
  ) 

png(file="output/Anomalyspiral.png",
    width=200, height=220, res=400, units="mm")
  print(plot)
dev.off()

## END 
############################################################################



