Skip to content

Instantly share code, notes, and snippets.

@brycemcd
Created July 21, 2015 01:59
Show Gist options
  • Save brycemcd/8a46239172271019f81e to your computer and use it in GitHub Desktop.
Save brycemcd/8a46239172271019f81e to your computer and use it in GitHub Desktop.
Comparing weather in Portland Oregon with New York City
library('dplyr')
library('tidyr')
library('ggplot2')
temperature_data <- function(dataset, stationName) {
dataset %>%
filter(STATION_NAME == stationName) %>%
mutate(dailyrange = (TMAXF-TMINF)) %>%
select(DATE, TMAXF, TMINF, dailyrange)
}
convert_c_to_f <- function(temp_in_c) {
temp_in_c * 9/5 + 32
}
clean_data <- function(dataSet) {
dataSet$DATE <- ymd(dataSet$DATE)
dataSet$TMAXC <- dataSet$TMAX / 10
dataSet$TMAXF <- convert_c_to_f(dataSet$TMAXC)
dataSet$TMINC <- dataSet$TMIN / 10
dataSet$TMINF <- convert_c_to_f(dataSet$TMINC)
dataSet
}
beav <- read.csv("~/Stats/weather/569578.csv", stringsAsFactors=FALSE)
beav <- clean_data(beav)
nyc <- read.csv("~/Stats/weather/569626.csv", stringsAsFactors=FALSE)
nyc <- clean_data(nyc)
pdx2012to2015 <- temperature_data(beav, "PORTLAND NWSFO OR US")
nyc2012to2015 <- temperature_data(nyc, "NEW YORK J F KENNEDY INTERNATIONAL AIRPORT NY US")
ggplot(data=nyc2012to2015, aes(x=DATE)) +
geom_ribbon(aes(ymax=TMAXF, ymin=TMINF), fill='darkblue', alpha=0.5) +
geom_ribbon(data=pdx2012to2015, aes(ymax=TMAXF, ymin=TMINF), fill='darkgreen', alpha=0.5) +
theme_bw() +
labs(title="Weather Comparison From 2012 to 2015 - Portland and New York City",
x = "Date (year)",
y = "Temperature (F)")
# looks like the range is quite a bit different
combined_weather <- inner_join(pdx2012to2015, nyc2012to2015, by = 'DATE')
ggplot(data=combined_weather, aes(x=DATE)) +
geom_line(aes(y=dailyrange.x-dailyrange.y), color='grey40', alpha=0.3) +
geom_smooth(se=FALSE, aes(y=(dailyrange.x-dailyrange.y)), color='black', size=2) +
theme_bw() +
labs(title="Difference in Range of Temperature Between NYC and PDX",
x = "Date (year)",
y = "Difference in Range of Temperature (F)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment