############################################################################
## Purpose: Animated figure to accompany the Lancet Regional Health Europe Comment entitled "Approaching unsafe limits: climate-related health inequities"
## Code adapted from Prof. Pat Schloss climate spiral R code: https://github.com/riffomonas/climate_viz/blob/95168349ece3c1993e960767a4b4d6124b4abd4a/code/climate_spiral_animation.R 
##
## 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: 10°W-40°E, 30-75°N
##
############################################################################

## load packages 
library(readxl)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(gganimate)
library(gifski)

## 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_data <- bind_rows(t_diff, next_jan) %>%
  mutate(month = factor(month, levels = c(month.abb, "next_Jan")),
         month_number = as.numeric(month)) %>%
  arrange(year, month) %>%
  mutate(step_number = 1:nrow(.))

## 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.5
)

## create individual frames of the climate spiral 
a <- t_data %>% 
  ggplot(aes(x=month_number, y=tem_dif, group=year, color=year)) +
  geom_rect(aes(xmin=1, xmax=13, ymin=-6, ymax=6),
            color="white", fill="white",
            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_hline(yintercept = 0, color = "green", size = 0.6)+
  geom_label(data = temp_lines, aes(x=x, y=y, label=labels),
             color = "black", fill = "#72bcd4", label.size = 0,
             inherit.aes=FALSE) +
  geom_text(data = month_labels, aes(x=x, y=y, label = labels),
            inherit.aes = FALSE, color="#4682B4",
            angle = seq(360 - 360/12, 0, length.out = 12), 
            size=4.5) +
  geom_line() +
  geom_label(aes(x = 1, y=-2.2, label = year),
             color="black", fill="#e6e6fa",
             label.size = 0, 
             size=4.5) +
  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(-3, 4, 1),
                     limits = c(-2.2, 4), expand = c(0, 0), 
                     sec.axis = dup_axis(name = NULL, labels=NULL)) +
  scale_color_viridis_c(option = "G",breaks = seq(1950, 2023, 20), 
                        guide = "none") +
  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 = -18, size = 15)
  ) + transition_manual(frames = year, cumulative = TRUE)

## create .gif out of the inividual frames 
animate(a, width=8, height=8, unit="in", res=300, renderer = gifski_renderer("output/Animated_Anomalyspiral.gif"))

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


