Last active
August 29, 2015 14:25
-
-
Save mrecos/40debdc7810c056bc0a9 to your computer and use it in GitHub Desktop.
A ggplot2 implementation of Tufte and Olson's city temperature charts
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
# Here is my attempt to use ggplot2 to recreate Randy Olson's (@randal_olson), recreation of the famous Tufte New York City Weather chart. | |
# The original Tufte chart is here: | |
# http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=00014g | |
# Randy Olson recreated this chart for a post on 538 (http://fivethirtyeight.com/) linked here: | |
# http://fivethirtyeight.com/features/what-12-months-of-record-setting-temperatures-looks-like-across-the-u-s/?ex_cid=538twitter | |
# and Randy posted his data and Python code here: | |
# https://github.com/mrecos/data/tree/master/us-weather-history | |
# This is a ggplot implementation of Randy's basic design, but without the top and right side axis and legend | |
# I think the legend in Randy's plot may be added in Illustrator of something | |
# The only real hard-code (aside from .csv location) here is the y-axis labels that show the data is from July '14 to June '15, | |
# this will still work of any of the data fro the 538 github page, but if you have new data with a different | |
# date range, you will need to adjust this. | |
# this was done as a quick learning example so sloppy coding and other errors may be expected. | |
# If you find it enjoyable, please let me know! @md_harris | |
require(ggplot2) # plotting package | |
require(Cairo) # for saving plot | |
# Helper function for x axis labels | |
addline_format <- function(x,...){ | |
gsub('\\s','\n',x) | |
} | |
# read in data | |
dat <- read.csv("{YOUR LOCATION}/us-weather-history/KPHL.csv") | |
city <- "Philadelphia" | |
# create an index | |
dat$day <- seq(1,365,1) | |
# find temp records | |
new_max_records = dat[which(dat$actual_max_temp >= dat$record_max_temp),] | |
new_min_records = dat[which(dat$actual_min_temp <= dat$record_min_temp),] | |
# set breaks and label for x axis | |
# this is rather static and could be redone dynamically using the date field | |
month_breaks = round(c(seq(1,365,(365/11)),365)) | |
month_labels = c("July '14","August","September","October","November", "December", | |
"January '15", "February", "March", "April", "May", "June") | |
# set range and lables for y axis | |
min_temp <- round(range(dat$record_min_temp)[1],-1) | |
max_temp <- round(range(dat$record_max_temp)[2],-1) | |
temp_range <- seq(min_temp, max_temp, by=10) | |
degree_lables <- parse(text = paste(temp_range, "*degree", sep = "")) | |
# build plot | |
p <- ggplot() + | |
geom_rect(data = dat, aes(xmin = day - 1, xmax = day, | |
ymin = record_min_temp, ymax = record_max_temp), | |
fill = '#C3BBA4', color = '#C3BBA4') + | |
geom_rect(data = dat, aes(xmin = day - 1, xmax = day, | |
ymin = average_min_temp, ymax = average_max_temp), | |
fill = '#9A9180', color = '#9A9180') + | |
geom_rect(data = dat, aes(xmin = day - 1, xmax = day, | |
ymin = actual_min_temp, ymax = actual_max_temp), | |
fill = '#5A3B49', color = 'black') + | |
geom_point(data = new_max_records, aes(x = day - 0.5, y = actual_max_temp + 2), | |
color='#d62728', alpha=0.75, size = 3) + | |
geom_point(data = new_min_records, aes(x = day - 0.5, y = actual_min_temp - 2), | |
color='#1f77b4', alpha=0.75, size = 3) + | |
scale_x_discrete(breaks = month_breaks, labels = addline_format(month_labels)) + | |
scale_y_continuous(breaks = temp_range, labels = degree_lables) + | |
labs(y=expression(paste("Temperature ( ", degree ~ F, " )")), title = city) + | |
theme( | |
rect = element_rect(fill="white", linetype=0, colour=NA), | |
text = element_text(family = "sans", face = "bold", color = "black", size = 14), | |
plot.title = element_text(size = rel(2)), | |
axis.text.x = element_text(vjust = 1, colou = "black", size = rel(1.25), face = "bold"), | |
axis.text.y = element_text(hjust = 1, color = "black", size = rel(1.25), face = "bold"), | |
axis.title.y = element_text(hjust = 0.5, vjust = 2, angle = 90, size = rel(1.25), face="bold"), | |
axis.title.x = element_blank(), | |
axis.ticks = element_line(colour="gray"), | |
panel.grid = element_line(colour=NULL, linetype = 1), | |
panel.grid.major = element_line(linetype = "dashed", colour = "gray"), | |
panel.grid.minor = element_blank(), | |
panel.background = element_rect(fill = "white")) | |
# save plot | |
plotName <- paste(c("PHL_temp.png"), collapse='') | |
CairoPNG(filename = plotName, width = 1220, height = 689, pointsize = 20, res=NA) | |
p | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment