Volcano Eruptions

Static map with ggplot2, Interactive map with leaflet, Animated map with gganimate

Published

May 11, 2020

Notable topics: Static map with ggplot2, Interactive map with leaflet, Animated map with gganimate

Recorded on: 2020-05-11

Timestamps by: Eric Fletcher

View code

Screencast

Timestamps

mutate
dplyr

Change the last_eruption_year into years_ago by using mutate from the dplyr package with years_ago = 2020 - as.numeric(last_eruption_year)). In the plot David includes +1 to account for 0 values in the years_ago variable.

str_detect
stringr

Use str_detect from the stringr package to search the volcano_name variable for Vesuvius when not sure if spelling is correct.

grom_pointtheme_mapborders
ggplot2ggthemes

Use the longitude and latitude to create a world map showing where the volcanoes are located.

fct_lump
forcats

Use fct_lump from theforcats package to lump together all primary_volcano_type factor levels except for the n most frequent.

str_remove
stringr

Use str_remove from the stringr package with the regular expression "\\(.\\)" to remove the parentheses.

leafletaddTilesaddCircleMarkers
leaflet

Use the leaflet package to create an interactive map with popup information about each volcano.

glue
glue

Use glue from the glue package to create an HTML string by concatenating volcano_name and primary_volcano_type between HTML <p></p> tags.

gathernestmap
DT

Use the DT package to turn the leaflet popup information into a datatable.

str_replacestr_to_title
stringr

Use str_replace_all fromt he stringr package to replace all the underscores _ in volcano_name with space. Then use str_to_title from the stringr package to convert the volcano_name variable to title case.

kable
knitr

Use kable with format = HTML from the knitr package instead of DT to make turning the data into HTML much easier.

paste0
base

Use paste0 from base R to bold the Volcano Name, Primary Volcano Type, and Last Eruption Year in the leaflet popup.

replace_na
tidyr

Use replace_na from the tidyr package to replace unknown with NA.

addMeasure
leaflet

Use addMeasure from the leaflet package to add a tool to the map that allows for the measuring of distance between points.

colorNumeric
leaflet

Use colorNumeric from the leaflet package to color the points based on their population within 5km. To accomplish this, David creates 2 new variables: 1) transformed_pop to get the population on a log2 scale & 2) pop_color which uses the colorNumeric function to generate the color hex values based on transformed_pop.

transition_timeframe_time
gganimate

Use the gganimate package to create an animated map.

geom_point
ggplot2

Use geom_point from the ggplot2 package with size = .00001 * 10 ^ vei so the size of the points are then proportional to the volume metrics provided in the Volcano Eruption Index. The metrics are in Km^3.

scale_size_continuous
ggplot2

Use scale_size_continuous from the ggplot2 package with range = c(.1, 6) to make the smaller points smaller and larger points larger.

scale_color_gradient2
ggplot2

Use scale_color_gradient2 from the ggplot2 package to apply color gradient to each point based on the volcano size and whether its low or high.

transition_reveal
gganimate

Summary of screencast while waiting for gganimate map to render.

Also, brief discussion on using transition_reveal instead of transition_time to keep the point on the map instead of replacing them in each frame.