Skip to content

Instantly share code, notes, and snippets.

@svmiller
Created June 20, 2017 15:24
Show Gist options
  • Save svmiller/4c7ffd09684280eaef330d6e94542c3b to your computer and use it in GitHub Desktop.
Save svmiller/4c7ffd09684280eaef330d6e94542c3b to your computer and use it in GitHub Desktop.
Get Phoenix airport June highs data
library(rnoaa)
library(tidyverse)
library(stringr)
library(weathermetrics)
my_token <- "get your own here..."
theme_steve <- function() {
theme_bw() +
theme(panel.border = element_blank(),
plot.caption=element_text(hjust=1, size=9,
margin=margin(t=10),
face="italic"),
plot.title=element_text(hjust=0, size=14,
margin=margin(b=10),
face="bold"),
axis.title.y=element_text(size=12,hjust=1,
face="italic"),
axis.title.x=element_text(hjust=1, size=12, face="italic"))
}
Phoenix <- ncdc(datasetid = 'GHCND', stationid = 'GHCND:USW00023183', startdate = '1940-06-01',
enddate = '1940-06-30', token = my_token, datatypeid=c("TMAX"), limit=40)$data
for (i in 1941:2016) {
start_date <- paste(i, "06", "01", sep="-")
end_date <- paste(i, "06", "30", sep="-")
dat <- ncdc(datasetid = 'GHCND',
stationid = 'GHCND:USW00023183',
startdate = start_date,
enddate = end_date,
token = my_token,
datatypeid=c("TMAX"),
limit=40)$data
Phoenix <- rbind(Phoenix,dat)
}
Phoenix %>%
mutate(year = as.numeric(str_sub(date, 1, 4))) %>%
group_by(year) %>%
summarize(high = mean(value)) %>%
mutate(high = celsius.to.fahrenheit(high/10)) %>%
ggplot(., aes(year, high)) + geom_line() + theme_steve() +
xlab("Year") + ylab("Average Monthly Highs") +
ggtitle("Average Highs in June for Phoenix Airport's NOAA Station, 1940-2016") +
scale_x_continuous(breaks=seq(1940, 2016, 4)) +
ylim(90, 115) +
labs(caption="Data: NOAA (via rnoaa package in R). Github: gist.github.com/svmiller") +
geom_smooth(method="loess")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment