Skip to content

Instantly share code, notes, and snippets.

@jdossgollin
Last active March 31, 2017 12:23
Show Gist options
  • Save jdossgollin/b19af83c4f4271013ef7eb4ace9c0eec to your computer and use it in GitHub Desktop.
Save jdossgollin/b19af83c4f4271013ef7eb4ace9c0eec to your computer and use it in GitHub Desktop.
Visualize the extent of sea ice over time. Based on 2010 script by Rasmus E. Benestad. Reference for data: Fetterer, F., K. Knowles, W. Meier, and M. Savoie. 2002, updated 2009. Sea Ice Index. Boulder, CO: National Snow and Ice Data Center. Digital media. http://nsidc.org/data/g02135.html
---
title: "Sea Ice"
output:
html_document: default
html_notebook: default
---
First load up any relevant packages.
This script uses the `pacman` package to make sure any required packages are ready to go -- it's a nice tool.
```{r}
if(!require(pacman)) install.packages('pacman')
pacman::p_load(jpeg, tidyverse, readr, lubridate, ggplot2, ggthemes)
```
Next set up the raw files
```{r}
ftp1 <- "ftp://sidads.colorado.edu/pub/DATASETS/NOAA/G02135/north/daily/data/N_seaice_extent_daily_v2.1.csv"
image.name <- "http://www.realclimate.org/images//sea-ice-Hudson-Bay-Dec12-2009.jpg"
```
We're ready to read in the raw data
```{r}
nisdc <- read_csv(ftp1, skip=2, col_names = c('year', 'month', 'day', 'extent', 'missing', 'source_data'),
col_types =
cols(
year = col_integer(),
month = col_integer(),
day = col_integer(),
extent = col_double(),
missing = col_double(),
source_data = col_character()
)) %>%
select(-source_data)
```
Did we do it?
```{r}
nisdc
```
To visualize we just need to do a couple of tricks on the data -- we need the day of year of each date
```{r}
nisdc <- nisdc %>%
mutate(date = ymd(paste(year, month, day))) %>%
mutate(doy = yday(date))
```
Cool, now we're ready to go!
For simplicity I'll leave off the background image.
The image is definitely a little easier to see, though the background image on the old one was kind of cool too.
```{r, fig.width=8}
plt1 <- ggplot(nisdc, aes(x = doy, y = extent)) +
geom_line(data = filter(nisdc, year <= 2011), aes(group = factor(year)), color='gray') +
geom_line(data = filter(nisdc, year > 2011), aes(color = factor(year))) +
scale_color_brewer(palette = 'Accent', name = "Year") +
theme_base(base_size = 10) +
labs(x = "Day of Year", y = "Arctic Sea Ice Extent [10^6 km^2]",
title = "Arctic Sea Ice Extent: 1978--Present",
subtitle = "Adapted from Rasmus Benestad at RealClimate.org",
caption = "Data from http://nsidc.org/data/g02135.html. Years 1978-2011 depicted in gray.")
plt1
```
This is nice, but it would be even cooler if it was interactive.
This is possible using the `plotly` package and their super-easy `ggplot` interface
```{r, fig.width=8}
pacman::p_load(plotly)
ggplotly(plt1)
```
This isn't quite as customized, but it's pretty cool to be able to see the raw data for each year!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment