Skip to content

Instantly share code, notes, and snippets.

@matteodefelice
Created June 14, 2025 19:40
Show Gist options
  • Select an option

  • Save matteodefelice/4e11973b054ee9a1592c4db8e8cb1a6d to your computer and use it in GitHub Desktop.

Select an option

Save matteodefelice/4e11973b054ee9a1592c4db8e8cb1a6d to your computer and use it in GitHub Desktop.
library(tidyverse)
# ERA5
e5 = read_csv('reanalysis-era5-single-levels-timeseries-sfc9vc2xo5f.csv') |>
mutate(
t2m = t2m - 273.15
) |>
group_by(
valid_time = floor_date(valid_time, unit = "days")
) |>
summarise(
# t2m = mean(t2m),
tp = sum(tp) * 1000
) |>
ungroup() |>
filter(dplyr::between(year(valid_time), 1950, 2014)) |>
mutate(
tp_a5 = tp > 5,
tp_a20 = tp > 20,
tp_a50 = tp > 50
)
ey = e5 |>
group_by(year = year(valid_time)) |>
summarise(
n5 = {
r <- rle(tp_a5)
sum(r$values) # Counts all TRUE sequences
},
n20 = {
r <- rle(tp_a20)
sum(r$values) # Counts all TRUE sequences
},
n50 = {
r <- rle(tp_a50)
sum(r$values) # Counts all TRUE sequences
},
annual = sum(tp)
) |>
ungroup() |>
mutate(
year_class = cut(year, breaks = 3)
)
g = ggplot(ey, aes(x = n20, y = annual, color = year_class )) +
geom_point(position = position_jitter(width = 0.2), size = 5) +
scale_color_brewer(palette = 'Reds') +
facet_wrap(~year_class, ncol = 1) +
geom_smooth(method = 'lm') +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment