Exercise 4: Plotting with leaflet

You will need leaflet installed to run the code. If you do not have it installed, run install.packages("leaflet")

The fourth map also requires leaflet.extras installed. If you do not have it installed, run install.packages("leaflet.extras")

Use knit to run this code.

# The file mg.csv contains the following information for Typhoon Mangkhut:
#     - lat: latitude
#     - lon: longitude (fixed)
#     - dt: Date time in format "%Y%b%d %H%M%S"
mg <- read.csv("mg.csv")

# Sample of the data:
mg[1:5,]
##   X.1  X               dt   lat    lon Intensity
## 1  59 59 2018SEP07 060000 12.10 166.20       2.2
## 2  60 60 2018SEP07 063000 12.03 166.16       2.3
## 3  61 61 2018SEP07 070000 11.98 166.11       2.3
## 4  62 62 2018SEP07 073000 11.95 166.04       2.4
## 5  63 63 2018SEP07 080000 11.94 165.96       2.4
# Load leaflet for plotting
library(leaflet)

A basic map using markers

# Plot out the data -- basic graph
leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addMarkers(data=mg, lng=~lon, lat=~lat, label = ~dt)

A map using clustered markers

# Plot out the data -- clustered graph
leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addMarkers(data=mg, lng=~lon, lat=~lat, label = ~dt,
             clusterOptions = markerClusterOptions())

A map using circle markers

# Plot out the data using circles
leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addCircleMarkers(data=mg, lng=~lon, lat=~lat, label = ~dt,
                   stroke = TRUE, radius=2, color="red")

Coloring the circle markers by intensity

leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addCircleMarkers(data=mg[mg$Intensity == 0,], lng=~lon, lat=~lat,  # on land
                   stroke = TRUE, radius=2, color="green", label=~paste0(dt,':', 'On land')) %>%
  addCircleMarkers(data=mg[mg$Intensity > 0 & mg$Intensity < 3.5,], lng=~lon, lat=~lat,  # Tropical storm or lower
                   stroke = TRUE, radius=2, color="blue", label=~paste0(dt,':', 'Tropical storm (or lower)')) %>%
  addCircleMarkers(data=mg[mg$Intensity >= 3.5 & mg$Intensity < 6,], lng=~lon, lat=~lat,  # Typhoon
                   stroke = TRUE, radius=2, color="orange", label=~paste0(dt,':', 'Typhoon')) %>%
  addCircleMarkers(data=mg[mg$Intensity >= 6,], lng=~lon, lat=~lat,  # Super typhoon
                   stroke = TRUE, radius=2, color="red", label=~paste0(dt,':', 'Super typhoon'))

Adding a bit of flair

# Blinking plot
library(leaflet.extras)

ty_icons <- pulseIcons(color='red',  #  supports hex colors like "#C0C0C0"
  heartbeat = 1.2,  #heartbeat is frequency of pulsing
  iconSize=3)

leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addCircleMarkers(data=mg[mg$Intensity == 0,], lng=~lon, lat=~lat,  # on land
                   stroke = TRUE, radius=2, color="green", label=~paste0(dt,':', 'On land')) %>%
  addCircleMarkers(data=mg[mg$Intensity > 0 & mg$Intensity < 3.5,], lng=~lon, lat=~lat,  # Tropical storm or lower
                   stroke = TRUE, radius=2, color="blue", label=~paste0(dt,':', 'Tropical storm (or lower)')) %>%
  addCircleMarkers(data=mg[mg$Intensity >= 3.5 & mg$Intensity < 6,], lng=~lon, lat=~lat,  # Typhoon
                   stroke = TRUE, radius=2, color="orange", label=~paste0(dt,':', 'Typhoon')) %>%
  addCircleMarkers(data=mg[mg$Intensity >= 6,], lng=~lon, lat=~lat,  # Super typhoon
                   stroke = TRUE, radius=2, color="red", label=~paste0(dt,':', 'Super typhoon')) %>%
  addPulseMarkers(data=mg[mg$Intensity >= 6 & mg$X %% 5 == 0,], lng=~lon, lat=~lat,  # Super typhoon
                   icon=ty_icons, label=~paste0(dt,':', 'Super typhoon'))