This map shows the 82 Cascade Volcanoes, formed as a result of subduction along the Cascadia subduction zone in the Pacific Northwest of North America.

This document corresponds with the interactive Leaflet map required at the peer-graded assignment for the “Developing Data Products” course on Coursera.

A desktop wallpaper image of the Mount Adams was the inspiration for this project. I picked up the Wikipedia’s list of Cascade Volcanoes (with location coordinates) and convert the data to a CSV format suitable for loading from R.

Building the map

Getting the data

The data is loaded directly from the CSV file (available in the same GitHub repository):

# CSV read
cascade_volcanoes <- read.csv("Cascade_Volcanoes.csv")

Explanatory Data Analysis

The volcanoes data consists in 10 variables. We will use the Latitude and Longitude for the position markers, the Volcanic Explosivity Index for the markers color and text. The rest of the information will be shown in the popups.

str(cascade_volcanoes)
## 'data.frame':    84 obs. of  10 variables:
##  $ Unknown         : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ State           : Factor w/ 4 levels "British Columbia",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Name            : Factor w/ 82 levels "Aspen Butte",..: 68 22 9 47 63 40 8 41 39 70 ...
##  $ Type            : Factor w/ 16 levels "Caldera","Cinder cone",..: 1 1 15 4 14 12 16 16 13 12 ...
##  $ ElevationMeters : int  2865 2252 2500 2645 2192 2385 2213 2162 1757 2319 ...
##  $ ElevationFeets  : int  9400 7388 8500 8678 7192 7825 7260 7093 5764 7608 ...
##  $ LastEruptionDate: Factor w/ 43 levels "-","1,000 BP",..: 43 42 43 14 43 16 41 41 41 41 ...
##  $ LastEruptionVEI : Factor w/ 8 levels "1?","2","2?",..: 8 8 8 7 8 8 8 8 8 8 ...
##  $ Latitude        : num  51.4 51.3 50.8 50.6 50.1 ...
##  $ Longitude       : num  -126 -125 -123 -124 -123 ...

The map

We want to show the volcanoes position over a topography map, so we choose the “Esri.NatGeoWorldMap”. The map will be centered at the Mount Adams.

Each marker will have the VEI number if it’s available and have a color associated with this VEI (a legend is shown in the corner). As seen in the Leaflet documentation, we will use auxiliary functions to configure the color and labels for the AwesomeIcons markers.

We will use popups to show aditional information about the volcanoes: name, type, elevation, last eruption date and VEI.

# Library load
library("leaflet")

# This funcion sets the icon color by type inside the 'awesomeIcons'
getColor <- function( vei ) {
  sapply(cascade_volcanoes$LastEruptionVEI, function(vei) {
  if( vei == "1" || vei == "1?" ) {
    "green"
  } else if( vei == "2" || vei == "2?") {
    "lightgreen"
  } else if( vei == "3" || vei == "3?") {
    "orange"
  } else if( vei == "4" || vei == "4?") {
    "red"
  } else if( vei == "5" || vei == "5?") {
    "purple"
  } else {
    "lightgray"
  } })
}

# This function sets the icon text
getName <- function( vei )
{
  sapply(cascade_volcanoes$LastEruptionVEI, function(vei) {
    if( vei == "Unknown") {
      "-"
    } else {
      paste( "<b>", vei, "</b>" )
    }
  })
}

# Creates the icons
icons <- awesomeIcons(
  text = getName( cascade_volcanoes ),
  iconColor = "black",
  markerColor = getColor( cascade_volcanoes )
)

# Generate the interactive map:
cv_plot <- leaflet( cascade_volcanoes, width = "100%" ) %>%
  addProviderTiles( providers$Esri.NatGeoWorldMap ) %>%
  setView(lng = -121.49, lat = 46.206, zoom = 9) %>%
  addAwesomeMarkers( lng   = ~Longitude,
              lat   = ~Latitude,
              # color = ~pal(Type),
              icon  = icons,
              popup = ~paste("<h4>", Name, "</h4>",
                             "<br/>Type: <b>", Type, "</b><br/>",
                             "Elevation: ", ElevationFeets, "feets (",
                             ElevationMeters, "m)<br/>",
                             "Last eruption date: ", LastEruptionDate, "<br/>",
                             "Last eruption VEI: <tt>",
                             LastEruptionVEI, "</tt>") ) %>%
  addLegend( position = 'topright',
              title = "Last eruption VEI",
              colors = c("#71AF26", "#BBF970", "#F69730", "#D53E2A", "#CB4FB3", "#B0B0B0"), 
              labels = c("1", "2", "3", "4", "5+", "Unknown"),
              opacity = 0.7 ) %>%
  addMiniMap( position = 'bottomright', toggleDisplay = TRUE )

# Print it!
cv_plot