Skip to content

Instantly share code, notes, and snippets.

@PMassicotte
Created October 7, 2020 14:41
Show Gist options
  • Save PMassicotte/89937576d37d229d55d9d62f2658b21c to your computer and use it in GitHub Desktop.
Save PMassicotte/89937576d37d229d55d9d62f2658b21c to your computer and use it in GitHub Desktop.
library(tidyverse)
library(curl)
library(ggpmthemes)
library(ggtext)
theme_set(theme_exo())
file <-
"ftp://sidads.colorado.edu/DATASETS/NOAA/G02135/north/daily/data/N_seaice_extent_daily_v3.0.csv"
raw_data <- curl_fetch_memory(file)
sea_ice_extent <- rawToChar(raw_data$content)
sea_ice_extent <- sea_ice_extent %>%
read_csv(
skip = 2,
col_names = c("year", "month", "day", "extent", "missing", "source")
) %>%
dplyr::select(year:extent) %>%
mutate(month = parse_number(month)) %>%
mutate(month2 = month.name[month]) %>%
mutate(month2 = factor(month2, month.name))
df_viz <- sea_ice_extent %>%
select(-month2) %>%
mutate(date = lubridate::make_date(year, month, day)) %>%
mutate(yday = lubridate::yday(date))
df_viz
p <- df_viz %>%
ggplot(aes(x = yday, y = extent, group = year)) +
geom_line(size = 0.1, color = "#A0A5A7", alpha = 0.5) +
geom_line(data = df_viz %>% filter(year == 2020), color = "#e63946", size = 0.5) +
stat_summary(fun = mean, colour = "#4281a4", geom = "line", aes(group = 1), size = 0.5) +
labs(
y = bquote("Ice extent" ~ (km^2 %*% 10^6)),
x = "Days of the year",
caption = paste("Source:", file, "\nVisualization: @philmassicotte"),
title = "In 2020, Arctic sea ice hits second-lowest level on record",
subtitle = "The <span style='color:#4281a4'>**red line**</span> represents the year 2020. The <span style='color:#457b9d'>**blue line**</span> represents the average."
) +
theme(
panel.border = element_blank(),
axis.ticks = element_blank(),
plot.caption = element_text(size = 4, lineheight = 1.25),
plot.title = element_text(size = 14),
plot.subtitle = element_markdown(size = 8),
panel.grid = element_line(size = 0.25),
panel.background = element_rect(fill = "#f0efeb"),
plot.background = element_rect(fill = "#f0efeb")
)
ggsave(
"~/Desktop/seaice_extent.png",
# device = ragg::agg_png,
# res = 600,
dpi = 600,
width = 6,
height = 4
)
# https://git.io/JUp4S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment