Skip to content

Instantly share code, notes, and snippets.

@mevers
Last active January 16, 2024 05:53
Show Gist options
  • Save mevers/66d778b59b451500fb6b62d730c51121 to your computer and use it in GitHub Desktop.
Save mevers/66d778b59b451500fb6b62d730c51121 to your computer and use it in GitHub Desktop.
Rainfall Canberra last decade
library(tidyverse)
library(hrbrthemes)
library(ggrepel)
library(weatherBOM)
# Return day of the season
sday <- function(date, season = "summer") {
switch(
season,
summer = if_else(month(date) %in% c(12, 1, 2), yday(date %m+% months(1)), NA),
autumn = if_else(month(date) %in% 3:5, yday(date %m-% months(2)), NA),
winter = if_else(month(date) %in% 6:8, yday(date %m-% months(5)), NA),
spring = if_else(month(date) %in% 9:11, yday(date %m-% months(8)), NA))
}
# Colours
# Source: https://tsitsul.in/blog/coloropt/
col_normal12 <- c(
"#EBAC23", "#B80058", "#008CF9",
"#006E00", "#00BBAD", "#D163E6",
"#B24502", "#FF9287", "#5954D6",
"#00C6F8", "#878500", "#00A76C",
"#BDBDBD")
# Get rainfall data using `https://github.com/mevers/weatherBOM`
data_plot <- weatherBOM::bom_historical_rain_data(070351) %>%
mutate(
day_in_summer = sday(date),
FY = 1L + year(floor_date(date, unit = "month") - months(6))) %>%
arrange(FY, day_in_summer) %>%
filter(FY >= 2015 & FY < 2025, !is.na(day_in_summer)) %>%
group_by(FY) %>%
mutate(cum_mm_rain = cumsum(replace_na(mm_rain, 0))) %>%
ungroup() %>%
mutate(FY = sprintf("%s/%s", FY - 2000 - 1, FY - 2000))
# Plot
data_plot %>%
ggplot(aes(day_in_summer, cum_mm_rain, colour = FY)) +
geom_step(linewidth = 1.01) +
geom_point(
data = data_plot %>%
filter(FY == "23/24") %>%
filter(day_in_summer == max(day_in_summer)),
size = 3,
show.legend = FALSE) +
geom_text_repel(
data = data_plot %>%
group_by(FY) %>%
filter(day_in_summer == max(day_in_summer)) %>%
ungroup(),
aes(label = FY),
nudge_x = +4,
nudge_y = +3,
segment.size = 0,
direction = "y") +
theme_ipsum_rc(plot_margin = margin(10, 10, 10, 10)) +
scale_colour_manual(values = rev(col_normal12[1:10])) +
scale_y_continuous(name = "Cummulative rain fall (in mm)") +
scale_x_continuous(
name = "Day in summer",
breaks = c(1, 15, 31 + 1, 31 + 15, 31 + 30 + 1, 31 + 30 + 15),
labels = c("Dec", "15", "Jan", "15", "Feb", "15"),
expand = expansion(mult = c(0, 0.1))) +
annotate("text", x = 25, y = -17, label = "\U1F384") +
coord_cartesian(ylim = c(0, 250), clip = "off") +
labs(
title = "2023/2024 is the wettest Canberra summer in (more than) a decade",
subtitle = "Cumulative rainfall during the three summer months, starting 1 Dec",
caption = paste(
"[Rainfall data source: Bureau of Meteorology, Canberra Airport station]",
"[Visualisation: https://gist.github.com/mevers/66d778b59b451500fb6b62d730c51121]",
sep = "\n")) +
theme(
plot.title.position = "plot",
plot.tag.position = c(25, -10),
legend.position = "none",
panel.grid.minor.x = element_blank(),
panel.background = element_rect(fill = 'white', colour = 'white'),
plot.background = element_rect(fill = 'white', colour = 'white'))
ggsave("why_does_it_always_rain_on_me.png", height = 7, width = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment