---
title: "Session 4 R practice, Exercise 4"
author: "Dr. Richard M. Crowley"
date: "ACCT 420, Fall 2019, Session 4"
output:
  html_document:
    theme: spacelab
    highlight: default
    toc: true
    toc_float:
      collapsed: false
    self_contained: true
---

## 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.

```{r}
# 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,]

# Load leaflet for plotting
library(leaflet)
```

### A basic map using markers

```{r}
# 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

```{r}
# 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

```{r}
# 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

```{r}
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

```{r}
# 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'))
```
