-
-
Save hrbrmstr/1849717a390142a7bf76aa5d6ea80577 to your computer and use it in GitHub Desktop.
R code to reproduce the awesome visualization of global temperature change from Ed Hawkins at http://www.climate-lab-book.ac.uk/2016/spiralling-global-temperatures/ using R and ggplot2 (with the animations package)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
library(animation) | |
#Data from https://crudata.uea.ac.uk/cru/data/temperature/ | |
#As well as data read in script | |
source("read_cru_hemi.R") | |
temp_dat <- read_cru_hemi("./HadCRUT4-gl.dat") | |
#remove cover | |
temp_dat_monthly <- temp_dat %>% | |
select(-starts_with("cover")) %>% | |
select(-starts_with("annual")) %>% | |
gather(month, anomaly, -year) %>% | |
mutate(month = gsub("month\\.", "", month)) %>% | |
mutate(month = as.numeric(month)) %>% | |
filter(year !=2016) | |
mo <- months(seq(as.Date("1910/1/1"), as.Date("1911/1/1"), "months")) | |
mo <- gsub("(^...).*", "\\1", mo) | |
saveGIF({ | |
for(i in 1850:2015){ | |
print(ggplot(temp_dat_monthly %>% filter(year <= i), | |
aes(x=month, y=anomaly, color=year, group=year)) + | |
geom_line() + | |
scale_color_gradient(low="blue", high="red", limits=c(1850, 2015), guide="none") + | |
geom_hline(yintercept=1.5, color="black", lty=2) + | |
geom_hline(yintercept=2, color="black", lty=2) + | |
coord_polar() + | |
annotate(x=1, y=-1.5, geom="text", label=i) + | |
annotate(x=1, y=1.5, geom="label", label="1.5C", fill="white", label.size=0) + | |
annotate(x=1, y=2, geom="label", label="2.0C", fill="white", label.size=0) + | |
ggtitle("Global Temperature Change 1850-2015") + | |
scale_x_continuous(labels=mo, breaks=1:13) + | |
scale_y_continuous(labels=NULL, breaks=NULL) + | |
ylab("") + xlab("") | |
)} | |
}, interval=0.1) | |
It's forked from https://gist.github.com/jebyrnes/b34930da0052a86f5ffe254ce9900357. I forked it to bookmark the data URLs to then make https://rud.is/b/2016/05/14/global-temperature-change-in-r-d3-without-the-vertigo/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this need read_cru_hemi.R to work?