Robert McDonnell

3 minute read

After seeing Nadieh Bremer’s great Breathing Earth infographic, I thought it would be cool to recreate it in R, as you do. Then I saw that it was made from lots of tif files…hmmm. I did some work with those before, ain’t doin it again voluntarily, no thanks.

So then I started thinking about something else that would be (kind of) similar and interesting. I saw the sf package and its interesting geom_sf() recently, and so I thought it would be a nice opportunity to try that out. Given we started with ‘breathing’ Earth, the natural next step was to think of ‘inhaling’ Earth! A quick download of some cannabis data from here and we’re (almost) ready to go, just some cleaning, tidying and merging with the geometry data from the rnaturalearth package.

library(stringi)
library(sf)
library(rnaturalearth)
library(readr)
library(dplyr)
library(ggplot2)

weed <- read_csv("/Users/robert/Downloads/drugs.csv", skip = 2) %>% 
  filter(!is.na(Year)) %>% 
  select(Region, Country, Rate = Best, Year) %>% 
  mutate(Year = ifelse(Year == "2013-2014", "2014", ifelse(
    Year == "2014/15", "2014", ifelse(
      Year == "2013/14", "2014", ifelse(
        Year == "2012/13", "2012", Year)))),
    Year = paste0(Year, "-01-01"), 
    Year = lubridate::parse_date_time(Year, "Ymd"),
    Country = stri_trans_general(Country, "Latin-ASCII"),
    Country = case_when(
      .$Country == "Venezuela (Bolivarian Republic of)" ~ "Venezuela",
      grepl("United Kingdom ", .$Country) ~ "United Kingdom",
      .$Country == "The former Yugoslav Republic of Macedonia" ~ "Macedonia",
      .$Country == "Taiwan Province of China" ~ "Taiwan",
      .$Country == "Russian Federation" ~ "Russia",
      .$Country == "Lao People's Democratic Republic" ~ "Laos",
      grepl("China,", .$Country) ~ "China",
      .$Country == "Bolivia (Plurinational State of)" ~ "Bolivia",
      TRUE ~ .$Country),
    Period = ifelse(Year < "2006-01-01", "2000-2005", ifelse(
      Year > "2010-01-01", "2010-2015", "2006-2010")))

globe <- ne_countries(scale = 110, type = "countries", returnclass =  "sf") %>% 
  select(Country = admin, formal = formal_en, geometry)

two <- full_join(weed, globe) %>% st_as_sf() %>%
  filter(!is.na(formal))

pp <- ggplot(two, aes(frame = Period)) + geom_sf(aes(fill = Rate)) +
  scale_fill_continuous(trans = "reverse", low = "dodgerblue4", high = "white") +
  theme_minimal()

animation::ani.options(ani.width = 800, ani.height = 600)
gganimate::gg_animate(pp, interval = 3, "first.gif")

Hmm, not a good candidate for animations…

How about booze?

'%ni%' <- Negate('%in%')

booze <- read_csv("/Users/robert/Downloads/UNdata_Export_20170523_022116370.csv") %>% 
  select(-Nature, Country = `Reference Area`) %>% 
  filter(Country %ni% c("World", "Developing regions",
                      "Caucasus and Central Asia", "Developed regions",
                      "Eastern Asia", "Landlocked developing countries",
                      "Latin America and the Caribbean", 
                      "Least developed countries", "Oceania",
                      "Northern Africa", "South-eastern Asia",
                      "Small island developing States", "Sub-Saharan Africa",
                      "Western Asia", "Southern Asia")) %>% 
  mutate(Country = case_when(
      .$Country == "Venezuela (Bolivarian Republic of)" ~ "Venezuela",
      grepl("United Kingdom ", .$Country) ~ "United Kingdom",
      .$Country == "The former Yugoslav Republic of Macedonia" ~ "Macedonia",
      .$Country == "Taiwan Province of China" ~ "Taiwan",
      .$Country == "Russian Federation" ~ "Russia",
      .$Country == "Democratic People's Republic of Korea" ~ "North Korea",
      .$Country == "Lao People's Democratic Republic" ~ "Laos",
      .$Country == "Czechia" ~ "Czech Republic",
      .$Country == "Timor-Leste" ~ "East Timor",
      .$Country == "Viet Nam" ~ "Vietnam",
      .$Country == "Brunei Darussalam" ~ "Brunei",
      .$Country == "Bahamas" ~ "The Bahamas",
      .$Country == "Côte d'Ivoire"  ~ "Ivory Coast",
      .$Country == "Guinea-Bissau"  ~ "Guinea Bissau",
      .$Country == "Syrian Arab Republic"  ~ "Syria",
      .$Country == "Iran (Islamic Republic of)"  ~ "Iran",
      .$Country == "Republic of Korea"  ~ "South Korea",
      .$Country == "Congo"  ~ "Republic of Congo",
      .$Country == "Serbia"  ~ "Republic of Serbia",
      .$Country == "Moldova"  ~ "Republic of Moldova",
      .$Country == "Bolivia (Plurinational State of)" ~ "Bolivia",
      TRUE ~ .$Country)) %>% 
  full_join(globe) %>% filter(!is.na(formal))
  


b <- ggplot(booze, aes(frame = `Time Period`)) + geom_sf(aes(fill = Value)) +
  scale_fill_continuous(trans = "reverse", low = "firebrick", high = "white") +
  theme_minimal() + guides(fill = guide_legend(title="Litres of \nPure Alcohol"))

gganimate::gg_animate(b, interval = 3, ani.width=800, ani.height=600)

That works! Nice. Ok, it’s pretty simple, but given the hellish wrangling involved with some spatial polygon sets and administrative unit geographical data, I’m really impressed with how easy geom_sf() was to use.

Data from here.

  • Category
  • sf
comments powered by Disqus