Created
December 5, 2022 17:30
-
-
Save mschnetzer/65560a87575d148a2ca696d92d185cc8 to your computer and use it in GitHub Desktop.
Hitzetage in Wien und den Bundesländern mit ZAMG API (https://twitter.com/matschnetzer/status/1599781018575044608)
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
library(tidyverse) | |
library(lubridate) | |
library(httr) | |
library(jsonlite) | |
library(msthemes) | |
library(geojsonsf) | |
library(sf) | |
# Get data: https://dataset.api.hub.zamg.ac.at/app/station-new/historical/klima-v1-1d?anonymous=true or use API like here | |
stations <- tribble( | |
~id, ~name, | |
105, "Wien", | |
131, "Salzburg", | |
93, "Niederösterreich", | |
56, "Oberösterreich", | |
48, "Kärnten", | |
39, "Tirol", | |
30, "Steiermark", | |
22, "Burgenland", | |
15, "Vorarlberg" | |
) | |
res.api = GET(glue::glue("https://dataset.api.hub.zamg.ac.at/v1/station/historical/klima-v1-1d?parameters=tmax&start=1950-01-01&end=2022-12-01&station_ids=",paste0(stations$id,collapse = ","))) | |
data <- fromJSON(rawToChar(res.api$content)) | |
tempdat <- tribble(~time, ~id, ~tmax) | |
for (i in 1:nrow(stations)) { | |
add.dat <- data.frame(time = as.Date(data[["timestamps"]]), | |
id = as.numeric(data[["features"]][["properties"]][["station"]][[i]]), | |
tmax = data[["features"]][["properties"]][["parameters"]][["tmax"]][["data"]][[i]]) | |
tempdat <- bind_rows(tempdat, add.dat) | |
} | |
blmap <- geojson_sf("https://raw.githubusercontent.com/ginseng666/GeoJSON-TopoJSON-Austria/master/2021/simplified-95/laender_95_geo.json") | |
findat <- tempdat |> left_join(stations) |> | |
mutate(time = ymd(time)) |> | |
group_by(year(time), name, .drop = FALSE) |> | |
filter(tmax >= 30) |> count() |> | |
rename(year = `year(time)`) | |
plotdat <- blmap |> left_join(findat) | |
plotdat |> ggplot(aes(fill = n)) + | |
facet_wrap(~year, ncol = 10) + | |
geom_sf(linewidth = 0.05, color = "black") + | |
scale_fill_gradient(low = "white", high = "red", name = "Anzahl Hitzetage") + | |
guides(fill = guide_colorbar(direction = "horizontal", barheight = 0.5, | |
barwidth = 10, title.position = "top")) + | |
labs(title = "Hitzetage in Österreich", | |
subtitle = "Anzahl der Tage mit 30°C oder wärmer nach Bundesland", | |
caption = "Anm.: Messdaten der jeweiligen Landeshauptstadt\nQuelle: ZAMG. Grafik: @matschnetzer") + | |
theme_ms(grid = F, alttf = T) + | |
theme(axis.text = element_blank(), | |
strip.text = element_text(size = 7, family = "Raleway"), | |
legend.position = "none") | |
ggsave("hitzetage.png", width = 8, height = 5, dpi = 320) | |
# Vienna only | |
wien <- blmap |> filter(name == "Wien") | |
wiendat <- findat |> filter(name == "Wien") | |
wienplot <- wien |> left_join(wiendat) | |
wienplot |> ggplot(aes(fill = n)) + | |
facet_wrap(~year, ncol = 10) + | |
geom_sf(linewidth = 0.05, color = "black") + | |
geom_text(aes(x=16.37505, y= 48.20915, label = n), size = 5, family = "Hack", | |
fontface = "bold", color = "white", alpha = 0.8) + | |
scale_fill_gradient(low = "grey90", high = "red", name = "Anzahl Hitzetage") + | |
guides(fill = guide_colorbar(direction = "horizontal", barheight = 0.5, | |
barwidth = 10, title.position = "top")) + | |
labs(title = "Hitzetage in Wien", | |
x=NULL, y=NULL, | |
subtitle = "Anzahl der Tage mit 30°C oder wärmer", | |
caption = "Daten: ZAMG. Grafik: @matschnetzer") + | |
theme_ms(grid = F, alttf = T) + | |
theme(axis.text = element_blank(), | |
strip.text = element_text(size = 7, family = "Raleway"), | |
legend.position = "none") | |
ggsave("hitzetagewien.png", width = 7, height = 6, dpi = 320) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment