Weather Data, Plot, and Code

Adapted from a blog post by Austin Wehrwein and Cran page by Claus O. Wilke. Data from Weather Underground.

The R code for generating a plot like the above can be found on both Austin Wehrwein’ blog and this Cran page, but I have included it below as well, with some minor modifications and details on how to retrieve your own data from the Weather Underground.


Select region and custom time period on this Weather Underground page. Copy/paste tabular data into Excel (may need to remove additional column headers). Relabel dates to follow format “%d-%b-%y” or “1-Jan-2016”. Relabel column headers. Save as CSV.

Note: you can try scraping directly from R with this tutorial, but WU recently changed their URL scheme, so this technique may not work.

Your data should look something like this. Mean Temp (Col C) is what I plotted, but pretty much any numeric variable can be used.


library(viridis)     ## color palette
library(ggjoy) ## ridges
library(hrbrthemes) ## plot theme

Import and minor data cleaning:

pgh_weather <- read.csv("weather_data.csv") ## setwd() to your own
months <- c("December","November","October","September","August","July","June","May","April","March","February","January") ## need this string vector for sorting later
pgh_weather$months <- as.Date(pgh_weather$CST, format = "%d-%b-%y") %>%
months() %>%
as.factor() %>%
factor(., levels = months)
mins <- min(pgh_weather$Min.TemperatureF)
maxs <- max(pgh_weather$Max.TemperatureF)


## black and white
ggplot(pgh_weather,aes(x = Mean.TemperatureF,y=months,height=..density..))+
geom_joy(scale=3) +
scale_x_continuous(limits = c(mins,maxs))+
strip.text.y = element_text(angle = 180, hjust = 1))+
labs(title='Temperatures in Pittsburgh',
subtitle='Median temperatures (Fahrenheit) by month for 2016\nData: Original CSV from the Weather Underground', x = "Mean Tempterature [ºF]")
## in color
ggplot(pgh_weather, aes(x = `Mean.TemperatureF`, y = `months`, fill = ..x..)) +
geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01, gradient_lwd = 1.) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_y_discrete(expand = c(0.01, 0)) +
scale_fill_viridis(name = "Temp. [ºF]", option = "C") +
labs(title = 'Temperatures in Pittsburgh',
subtitle = 'Mean temperatures (Fahrenheit) by month for 2016\nData: Original CSV from the Weather Underground',
x = "Mean Temperature") +
theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank())

Next steps:

Download yearly data and trace rising temperatures with GIF or fading ridges.