Created
June 18, 2018 13:44
-
-
Save zjuul/cdca5eda284137638ace988f35e5042b to your computer and use it in GitHub Desktop.
KNMI NL weerdata graphing
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
# visualise weather in NL | |
# | |
# | |
# (c) Jules Stuifbergen 2018 - Creative Commons licence CC BY-SA | |
# | |
# | |
library(httr) | |
library(tidyverse) | |
library(lubridate) | |
library(viridis) | |
library(gganimate) | |
library(animation) | |
datafile <- "./weerdata.txt" | |
# download raw data | |
knmi_url <- 'http://projects.knmi.nl/klimatologie/daggegevens/getdata_dag.cgi' | |
knmi_params <- list(start = "19000101", | |
end = format(Sys.Date(), "%Y%m%d"), | |
vars = "TEMP", | |
stns = "260") #station 260 = De Bilt | |
knmi_req <- GET(knmi_url, query = knmi_params) # documentation says POST, but that doesn't work | |
# write datafile | |
write_file(content(knmi_req, as = "text"), datafile) | |
# output: | |
# STN,YYYYMMDD, TG, TN, TNH, TX, TXH, T10N,T10NH | |
# read datafile | |
knmi_raw <- read_csv(datafile, comment = "#", | |
col_names = c("station", "yyyymmdd", "mean", "min", "min_h", "max", "max_h", "t10_min", "t10_min_h")) | |
# cleanup raw datafile, select some vars, make date, convert to actual degrees | |
knmi <- knmi_raw %>% transmute( dag = as_date(as.character(yyyymmdd)), jaar = as.character(year(dag)), | |
doy = yday(dag), | |
gem = mean/10, min = min/10, min_h = min_h, | |
max = max/10, max_h = max_h, | |
t10_min = as.numeric(t10_min)/10, t10_min_h = t10_min_h, | |
soort_dag = case_when( max >= 30 ~ "tropische dag", | |
max >= 25 ~ "hete dag", | |
max >= 20 ~ "warme dag") | |
) | |
# plot some cool stuff | |
# animated barplot | |
p1 <- ggplot( knmi %>% mutate( max_temp = round(max)) %>% # vanwege de kleur moet dit even met de hand geteld worden | |
group_by(jaar, max_temp) %>% summarise(n = n()), | |
aes( x = max_temp, y = n, frame = jaar) ) + | |
geom_bar(aes(fill = max_temp), stat = "identity", position = "identity", width = 1) + | |
scale_fill_viridis(discrete = FALSE, option = "B") + | |
theme_minimal() + theme(legend.position = "none") + | |
xlab("maximum dagtemperatuur") + | |
ylab("aantal dagen dit jaar met deze temperatuur") | |
ani.options(interval = 0.25) | |
gganimate(p1) | |
# hot days - stacked barplot of freq | |
# dataset as is, count days (barplot) | |
ggplot( filter(knmi, !is.na(soort_dag)), aes( x = factor(jaar))) + | |
geom_bar(aes(fill = factor(soort_dag, levels = c("warme dag","hete dag","tropische dag"))), width=1) + | |
scale_fill_manual(values = c("tropische dag" = "red", "hete dag" = "darkorange", "warme dag" = "gold"), name = "soort dag") + | |
theme_minimal() + theme(axis.text.x=element_text(angle=60, hjust=1)) + | |
scale_x_discrete(breaks = seq(1900, 2020, 5)) + | |
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + | |
xlab("") + ylab("aantal van deze dagen in het gegeven jaar") + | |
ggtitle("Tropische, hete en warme dagen sinds 1900") | |
# line graph follows.. first count manually | |
linedata <- knmi %>% | |
filter(!is.na(soort_dag), jaar < max(jaar)) %>% # remove this (incomplete) year | |
group_by(jaar, soort_dag) %>% | |
summarise(aantal = n()) | |
ggplot(linedata, aes(x = jaar, y = aantal)) + | |
geom_line(aes(color = soort_dag, group = soort_dag)) + | |
geom_smooth(aes(color = soort_dag, group = soort_dag, alpha = .2), fill = "lightgrey", alpha = .6, linetype = "dotted") + | |
scale_color_manual(values = c("tropische dag" = "red", "hete dag" = "darkorange", "warme dag" = "gold"), name = "soort dag") + | |
theme_minimal() + theme(axis.text.x=element_text(angle=60, hjust=1)) + | |
scale_x_discrete(breaks = seq(1900, 2020, 5)) + | |
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + | |
xlab("") + ylab("aantal van deze dagen in het gegeven jaar") + | |
ggtitle("Tropische, hete en warme dagen sinds 1900") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment