Last active
August 11, 2020 15:45
-
-
Save matteodefelice/03b94a78e9d8d34bf26d66d64446346c to your computer and use it in GitHub Desktop.
Code used for the plot shared in https://twitter.com/matteodefelice/status/1293200381309259782
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(lubridate) | |
library(tidyverse) | |
library(zoo) | |
read_station <- function(filename) { | |
read_csv(filename, | |
col_types = cols(DATE = col_datetime(format = "%Y%m%d")), | |
skip = 20 | |
) | |
} | |
station_files <- c( | |
"tx/TX_STAID000037.txt", | |
"tx/TX_STAID000737.txt", | |
"tx/TX_STAID000786.txt", | |
"tx/TX_STAID002207.txt" | |
) | |
stn_id <- tribble( | |
~STAID, ~name, | |
37, "LYON - ST EXUPERY (FR)", | |
755, "EMBRUN (FR)", | |
737, "LILLE-LESQUIN (FR)", | |
786, "MONTELIMAR (FR)", | |
2207, "MONTPELLIER-AEROPORT (FR)" | |
) | |
stn_data <- lapply(station_files, read_station) %>% | |
bind_rows() %>% | |
mutate( | |
tmax = ifelse(TX != -9999, TX / 10, NA) | |
) | |
sel <- stn_data %>% | |
group_by(STAID, year = year(DATE)) %>% | |
summarise( | |
n35 = sum((tmax >= 35) & (tmax <37), na.rm = TRUE), | |
n37 = sum((tmax >= 37) & (tmax <40), na.rm = TRUE), | |
n40 = sum(tmax >= 40, na.rm = TRUE) | |
) %>% | |
gather( | |
var, ndays, -year, -STAID | |
) %>% | |
left_join( | |
stn_id, | |
by = "STAID" | |
) | |
sel %>% | |
dplyr::filter(ndays > 0) %>% | |
count(name, var) | |
g <- ggplot(sel, aes(x = year, y = ndays, fill = var)) + | |
geom_bar( | |
stat = "identity", | |
color = "grey66" | |
) + | |
scale_fill_manual( | |
values = c("#ffffb2", "#fd8d3c", "#bd0026"), | |
labels = c("35 <= TX < 37°", "37 <= TX < 40°", "TX >= 40°"), | |
name = "" | |
) + | |
hrbrthemes::theme_ipsum_rc() + | |
facet_wrap(~name, ncol = 1, scales = "free") + | |
labs( | |
y = "Number of days" | |
) + | |
coord_cartesian( | |
xlim = c(1950, 2019) | |
) + | |
scale_x_continuous(breaks = c(seq(1950, 2019, 10), 2019)) | |
ggsave("france-tmax.png", width = 8, height = 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment