suppressPackageStartupMessages({
  library(tidyverse)
  library(scales)
  library(glue)
  library(lubridate)
  library(gganimate)
  library(gifski)
  library(png)
  library(DT)
})

1 Data

The data used in this report is taken from this link.

read_cru_hemi <- function(filename) {
  # read in whole file as table
  tab <- read.table(filename,fill=TRUE)
  nrows <- nrow(tab)
  # create frame
  hemi <- data.frame(
    year=tab[seq(1,nrows,2),1],
    annual=tab[seq(1,nrows,2),14],
    month=array(tab[seq(1,nrows,2),2:13]),
    cover=array(tab[seq(2,nrows,2),2:13])
  )
}

url_dat <- "https://crudata.uea.ac.uk/cru/data/temperature/HadCRUT4-gl.dat"
temp_dat <- read_cru_hemi(url_dat)
min_year <- 1850
max_year <-2019

mo <- month(seq(as.Date("2000/1/1"), as.Date("2000/12/1"), "months"), label = TRUE, abbr = TRUE) %>% as.character()

temp_monthly <- 
  temp_dat %>%
  select(-annual, -starts_with("cover")) %>%
  pivot_longer(cols = starts_with("month"), names_to = "month", values_to = "anomaly",names_prefix = "month.")%>% 
  mutate(month = as.numeric(month)) %>% 
  filter(between(year, min_year,max_year))
p<-
  temp_monthly %>% 
  ggplot(aes(x=month, y=anomaly, color=year, group=year))+
  geom_line(alpha = 0.6, size = 0.8)+
  scale_color_gradient(low="royalblue", high="yellow", limits=c(min_year, max_year), guide = NULL) +
  geom_hline(yintercept=1.5, color="red", lty=2, size = 1.5) +
  geom_hline(yintercept=2, color="red", lty=1, size = 2)+
  scale_x_continuous(breaks=1:12, labels = mo) +
  scale_y_continuous(labels=NULL, breaks=NULL)+
  #geom_label(data = temp_monthly, aes(x = 1, y = -1.5, label = year), color = "white",fill="grey20", label.size = 0) +
  annotate(x=1, y=1.5, geom="label", label="1.5C", fill="grey20", color = "red", label.size = 0, size = 5) +
  annotate(x=1, y=2, geom="label", label="2.0C", fill="grey20", color = "red", label.size=0, size = 5) +
  theme_minimal()+
  theme(plot.background = element_rect(fill = "grey20"),
        axis.text.x = element_text(color = "white", size = 14),
        title = element_text(color = "white", size = 14),
        panel.grid = element_blank()
          )+
  labs(x="", y="", title = "Global Temperatures Change: {frame_along}",caption = "Made in R by Alex L.")+
  coord_polar()+
  transition_reveal(year)

if (FALSE){
  animate(p,duration = 30,fps = 10)
  anim_save("climate.gif")  
}else{
  animate(p, duration = 30,fps = 10, renderer = ffmpeg_renderer(options = list(loglevel = 'quiet')))
}