Created
May 9, 2017 10:30
-
-
Save cavedave/396ac23daabe32efa16431fb734c4a39 to your computer and use it in GitHub Desktop.
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
#This is inspired by this spiral animation of the same dataset | |
#http://www.climate-lab-book.ac.uk/2016/spiralling-global-temperatures/ | |
#and this R code to produce the animation | |
#https://gist.github.com/jebyrnes/b34930da0052a86f5ffe254ce9900357 | |
# It also uses elements of this http://www.r-bloggers.com/making-faceted-heatmaps-with-ggplot2/ | |
# and this https://rpubs.com/bradleyboehmke/weather_graphic graphic | |
library(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
library(animation) | |
library(viridis) | |
library(scales) | |
library(ggthemes) | |
#Data explanation at https://crudata.uea.ac.uk/cru/data/temperature/ | |
#r code to read data https://crudata.uea.ac.uk/cru/data/temperature/read_cru_hemi.r | |
# data https://crudata.uea.ac.uk/cru/data/temperature/HadCRUT4-gl.dat | |
#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) | |
dgr_fmt <- function(x, ...) { | |
parse(text = paste(x, "", sep = "")) | |
} | |
a <- dgr_fmt(seq(1850,2016, by=15)) | |
gg <- ggplot(temp_dat_monthly, aes(x=year, y=month, fill=anomaly)) | |
gg <- gg + geom_tile(color="white", size=0.1) | |
gg <- gg + scale_fill_viridis(name="Difference from \nAverage in °C",option="inferno") | |
plot.title = 'Average World Temperature Since 1850' | |
plot.subtitle = 'HadCRUT4-gl Data' | |
gg <- gg + ggtitle(bquote(atop(.(plot.title), atop(italic(.(plot.subtitle)), "")))) | |
gg <- gg + labs(x=NULL, y=NULL) | |
gg <- gg + theme_hc(base_family="Helvetica") | |
gg <- gg + | |
coord_cartesian(xlim = c(1850,2016)) + | |
scale_x_continuous(breaks = seq(1850,2016, by=15), labels = a) + | |
scale_y_continuous(expand = c(0, 0), | |
breaks = c(1,2,3,4,5,6,7,8,9,10,11,12), | |
labels = c("Jan", "Feb", "Mar", "Apr", | |
"May", "Jun", "Jul", "Aug", "Sep", | |
"Oct", "Nov", "Dec")) | |
gg <- gg + theme(plot.title=element_text(hjust=0.5)) | |
gg <- gg + theme(axis.text=element_text(size=7)) | |
gg <- gg+ theme(plot.title = element_text(size=22)) | |
gg <- gg + theme(axis.ticks=element_blank()) | |
ggsave("heat2017.png", width = 10, height = 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment