Skip to content

Instantly share code, notes, and snippets.

@oliviergimenez
Created April 27, 2024 20:58
Show Gist options
  • Save oliviergimenez/0aa662fc723742ccc0affe614c2ffd93 to your computer and use it in GitHub Desktop.
Save oliviergimenez/0aa662fc723742ccc0affe614c2ffd93 to your computer and use it in GitHub Desktop.
library(tidyverse)
url <- "https://www.metoffice.gov.uk/hadobs/hadcet/data/legacy/cetdl1772on.dat"
df_raw <- read_tsv(url, col_names = FALSE)
div_10 <- function(...){
.../10
}
df <- df_raw %>%
separate(X1, into = c("year", "day", month.abb)) %>%
mutate_at(.vars = vars(year:Dec), as.double) %>%
mutate_at(.vars = vars(Jan:Dec), div_10) %>%
pivot_longer(cols = Jan:Dec, names_to = "month", values_to = "temp") %>%
filter(year > 1900, temp < 99) %>%
mutate(date = glue::glue("{year}-{month}-{day}"),
date = as.Date(date, format = "%Y-%b-%d")) %>%
arrange(date)
df_sum <- df %>%
group_by(month, day) %>%
summarize(mean = mean(temp, na.rm = TRUE)) %>%
ungroup() %>%
mutate(date = glue::glue("2019-{month}-{day}"),
date = as.Date(date, format = "%Y-%b-%d")) %>%
arrange(date)
df_join <- df_sum %>%
select(date, mean)
(plot_test <- df_sum %>%
ggplot(aes(x = date, y = mean)) +
geom_path(group = 1))
plot_test +
scale_y_continuous(limits = c(0, 20))
df_diff <- df %>%
filter(year == 2019) %>%
left_join(df_join, by = "date") %>%
rowwise() %>%
mutate(diff = if_else(temp > mean, "warmer", "cooler"))
df_fill <- df %>%
filter(year == 2019) %>%
select(date, month, day) %>%
group_by(month) %>%
summarize(xmax = max(day),
xmin = min(day)) %>%
ungroup() %>%
mutate(fill = if_else(
month %in% c("Jan", "Mar", "May", "Jul", "Sep", "Nov"),
"lightgrey",
"grey"
)) %>%
mutate(date = glue::glue("2019-{month}-{xmax}"),
date = as.Date(date, format = "%Y-%b-%d")) %>%
arrange(date) %>%
mutate(ymin = -5, ymax = 26)
df_label <-
data.frame(
x = as.Date("2019-01-01"),
y =
)
(plot_year <- df_diff %>%
ggplot(aes(x = date, y = mean)) +
geom_segment(aes(x = date, xend = date, y = mean, yend = temp, color = diff),
lineend = "butt", size = 1.5) +
geom_path(group = 1, size = 1, color = "white", alpha = 0.2) +
geom_path(group = 1, size = 3, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 5, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 8, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 12, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 15, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 20, color = "white", alpha = 0.3) +
geom_path(group = 1, size = 1) +
annotate("text",
x = as.Date("2019-01-01"),
y = 4.57, label = 'bold("average days")',
parse = TRUE, hjust = 1.1) +
annotate("text",
x = as.Date("2019-01-01"),
y = 6.57, label = 'bold("warmer than\naverage days")',
parse = TRUE, hjust = 1.1, color = "#ff3232") +
annotate("text",
x = as.Date("2019-01-01"),
y = 1.57, label = 'bold("coolor than\naverage days")',
parse = TRUE, hjust = 1.1, color = "#6666ff") +
annotate("text",
x = as.Date("2019-01-01"),
y = 20,
label = 'bold("2019: A year in review")',
parse = TRUE, hjust = 0.2,
size = 12) +
scale_fill_manual(values = c("#6666ff", "#ff3232"), aesthetics = c("fill", "color")) +
theme_void() +
theme(plot.margin = margin(30, 30, 10, 150)) +
theme(legend.position = "none") +
scale_y_continuous(limits = c(-5, 26)) +
scale_x_date(date_labels = "%b", date_breaks = "1 month") +
theme(axis.text.x = element_text(face = "bold")) +
coord_cartesian(ylim = c(-5, 26), clip = "off", expand = FALSE) +
labs(x = "",y = "",
caption = "\nPlot: @thomas_mock"))
ggsave("temp-year.png", plot_year, height = 6, width = 12, units = "in", dpi = "retina")
@oliviergimenez
Copy link
Author

Capture d’écran 2024-04-27 à 22 58 08

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment