Skip to content

Instantly share code, notes, and snippets.

@jbryer
Created November 5, 2021 01:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jbryer/487177a7d777ce3fa3fc337d4d878761 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(reshape2)
library(lubridate)
library(rnoaa)
token <- '' # Get token here: https://www.ncdc.noaa.gov//cdo-web/token
options(noaakey = token)
marathon_dates <- c('2021-01-10','2020-01-12','2019-01-13',
'2018-01-07','2017-01-08','2016-01-10','2015-01-11','2014-01-12',
'2013-01-13','2012-01-08','2011-01-09','2010-01-10','2009-01-11',
'2008-01-13','2007-01-07','2006-01-08','2005-01-09','2004-01-11',
'2003-01-12','2002-01-06','2001-01-07','2000-01-09','1999-01-10',
'1998-01-11','1997-01-05','1996-01-07','1995-01-08','1994-01-16') %>%
as.Date()
# Find the closest weather station
x <- isd_stations_search(lat = 28.3772, lon = -81.5707, radius = 17)
x
weather <- data.frame(date = marathon_dates,
min_temp = NA_real_,
max_temp = NA_real_,
start_temp = NA_real_,
avg_temp = NA_real_,
avg_wind_speed = NA_real_) %>%
mutate(year = year(date))
for(i in 1:nrow(weather)) {
weather_data <- isd(usaf = x[1,]$usaf, wban = x[1,]$wban, year = year(weather[i,]$date)) %>%
dplyr::filter(temperature != '+9999') %>% ## remove +9999's
dplyr::mutate(temp_celsuis = as.numeric(temperature) / 10,
temp_fahrenheit = temp_celsuis * 9/5 +32,
date_time = parse_date_time(paste0(date, time), 'y-m-d HM', tz = 'America/New_York'),
date = as.Date(date, format = '%Y%m%d'),
wind_speed = 1.60934 * as.numeric(wind_speed) / 10,
time_from_start = abs(date_time - as.POSIXct(paste0(date, ' 05:30:00'), tz = 'America/New_York'))
) %>%
dplyr::filter(date == weather[i,]$date) %>%
select(date, time, date_time, temp_celsuis, temp_fahrenheit, wind_speed, time_from_start)
if(nrow(weather_data) > 0) {
weather[i,]$min_temp <- min(weather_data$temp_fahrenheit, na.rm = TRUE)
weather[i,]$max_temp <- max(weather_data$temp_fahrenheit, na.rm = TRUE)
weather[i,]$start_temp <- weather_data[weather_data$time_from_start == min(weather_data$time_from_start),]$temp_fahrenheit
weather[i,]$avg_temp <- mean(weather_data$temp_fahrenheit, na.rm = TRUE)
weather[i,]$avg_wind_speed <- mean(weather_data$wind_speed, na.rm = TRUE)
} else {
print(paste0('No data found for ', weather[i,]$date))
}
}
weather <- weather[complete.cases(weather),]
weather.melt <- melt(weather[,c('year', 'min_temp', 'max_temp', 'start_temp', 'avg_temp', 'avg_wind_speed')],
id.vars = c('year', 'start_temp', 'avg_temp', 'avg_wind_speed'))
ggplot(weather.melt, aes(x = year)) +
geom_ribbon(data = weather, aes(ymin = min_temp, ymax = max_temp), alpha = 0.3, fill = 'skyblue') +
geom_path(aes(y = start_temp, group = variable), color = 'maroon') +
geom_point(aes(y = start_temp), color = 'maroon') +
geom_path(aes(y = value, group = variable)) +
geom_point(aes(y = value, size = avg_wind_speed)) +
geom_text(data = weather, aes(y = max_temp, label = paste0(max_temp, '°')), vjust = -1) +
geom_text(data = weather, aes(y = min_temp, label = paste0(min_temp, '°')), vjust = 2) +
scale_x_continuous(breaks = seq(min(weather$year), max(weather$year))) +
scale_size_continuous('Wind\nSpeed\n(mph)') +
ylim(c(25, 85)) +
ylab('Temperature (Fahrenheit)') + xlab('') + theme_minimal() +
ggtitle('Disney Marathon Weather',
subtitle = 'Temperature range and starting temperatures (in red)')
ggsave('DisneyMarathonWeather.png', width = 10, height = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment