Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maptracker
Forked from cavedave/WorldHeat.r
Last active November 14, 2017 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptracker/ddb5cade904941481116c60a7d1afff3 to your computer and use it in GitHub Desktop.
Save maptracker/ddb5cade904941481116c60a7d1afff3 to your computer and use it in GitHub Desktop.
Heatmap of world Temperature in r package for ggplot2
### FORKED FROM: https://gist.github.com/cavedave/8ff22e94c882e9f6be933f99f2ae0b50
## Charles changed:
## Absolute URLs to library/data (rather than local files)
## Raster rather than tiles (didn't like the grid lines)
## Gradient from red (hot) to blue (cold) with black at zero (no change from average)
## Removed year filter
## Defined title and breaks via automatic extraction of dates from data
## Scaled down plot to be more friendly for Gist
## Took out unused libraries
## install.packages(c("dplyr", "tidyr", "ggplot2", "ggthemes"))
#This is inspired by this piral 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(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("https://crudata.uea.ac.uk/cru/data/temperature/read_cru_hemi.r")
temp_dat <- read_cru_hemi("https://crudata.uea.ac.uk/cru/data/temperature/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))
dgr_fmt <- function(x, ...) {
parse(text = paste(x, "", sep = ""))
}
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
minYr <- min(temp_dat_monthly$year)
maxYr <- max(temp_dat_monthly$year)
maxMon <- max(temp_dat_monthly[temp_dat_monthly$year == maxYr &
!is.na(temp_dat_monthly$anomaly), "month"] )
brk <- seq(minYr, maxYr, by=10)
gg <- ggplot(temp_dat_monthly, aes(x=month, y=year, fill=anomaly))
gg <- gg + geom_raster()
gg <- gg + scale_fill_gradient2(name="Difference from \nAverage in °C", na.value="white",
mid="black", midpoint=0, low="blue", high="red")
plot.title = paste('Average World Temperature from', minYr,
'to', months[ maxMon ], maxYr)
plot.subtitle = 'Data is HadCRUT4-gl from crudata'
gg <- gg + ggtitle(bquote(atop(.(plot.title), atop(italic(.(plot.subtitle)), ""))))
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + theme_tufte(base_family="Helvetica")
gg <- gg +
coord_cartesian(ylim = c(1850,2015)) +
scale_y_continuous(breaks = brk, labels = brk) +
scale_x_continuous(expand = c(0, 0),
breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
labels = months)
gg <- gg + theme(plot.title=element_text(hjust=0))
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_text(size=7))
ggsave("heat.png", width=5, height=3, dpi=150)
print(gg)
@maptracker
Copy link
Author

maptracker commented Nov 14, 2017

heat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment