Objectives


Set up

As usual, I’ll be using the EPI dataset. In this tutorial, I am filtering the full dataset to years when the TCL variable is available and a small subset of 7 countries:

library(tidyverse)
epi <- readRDS("./data/epir.RDS")

tcl <- epi %>% filter(year %in% 2001:2016) %>% select(year, continent, country, TCL) %>%
  filter(country %in% c("United States", "Brazil", "Canada", "Belgium", "Madagascar", "Peru", "Mexico"))

Heat maps

The geom_tile() geometry allows us to visualize values of a variable through time and by groups. This basically divides the data into discrete categories (boxes) by time and assigns a color to each category (box) based on the value of the variable of interest. The easiest way to understand what heat maps to is to look at one, so let’s plot changes in TCL by country through time:

ggplot(data = tcl) +
  geom_tile(aes(x = year, y = country, fill = TCL))

Each row in the plot represents a country. Columns represent years. The color of blue in the table represents the value of tree cover loss for that county-year combination. We can see that Madagascar appears to have the highest rates of TCL, and that these rates grew significantly through time. Let’s do a few quick tweaks to make the plot look better:

ggplot(data = tcl) +
  geom_tile(aes(x = year, y = country, fill = TCL)) +
  scale_fill_gradient(name = "TCL", 
                      low = "#f0f4ed",
                      high = "#496160" ) +
  theme_minimal() +
  xlab("") +
  ylab("") +
  scale_x_continuous(breaks = seq(from = 2001, to = 2015, by = 2))

The color palette isn’t great, but this gives you an idea of how to customize a color palette based on the colors of your final project. In the scale_fill_gradient() function, you need to list the variable you want to symbolize (in our case, TCL), and the low to high colors you want to use to define your color ramp. Here I go from light gray to dark gray.

To reorder by the values of the last column, try this:

reo = tcl %>% filter(year == "2014") # replace year with your last year in heat map
tcl$REO_COUNTRY = factor(tcl$country, levels = reo$country[order(reo$TCL)]) # replace TCL with your VOI

ggplot(data = tcl) +
   geom_tile(aes(x = year, y = REO_COUNTRY, fill = TCL)) +
  scale_fill_gradient(name = "TCL", 
                      low = "#f0f4ed",
                      high = "#496160" ) +
  theme_minimal() +
  xlab("") +
  ylab("") +
  scale_x_continuous(breaks = seq(from = 2001, to = 2015, by = 2))


Line plots

Another useful way to visualize change through time is with line plots. We’ve already made line plots in this class, but below, I’ll show you how to highlight specific lines to tell a more interesting story about change through time. First, let’s make a basic line plot and also add the actual data with geom_point():

ggplot(data = tcl) +
  geom_point(aes(x = year, y = TCL, color = country)) +
  geom_line(aes(x = year, y = TCL, color = country))