AAEC8610 HW5 Option 2 : Leaflet in R.

(Submitted to Dr. Filipski)

Akash [If we have the moon and the night, we need to rest]

2020-02-09 17:04:06

Optional task 2: Interactive Maps with Leaflet

This is theSecond task of the 5th Homework for the Ph.D. course AAEC8610 (Advanced Econometrics) with Dr. Mateusz Filipski. Here, I have used the package {library(leaflet)} and {library(sf)} to explore.1

  • Install “leaflet” package: install.packages("leaflet")

1. Get familiar with the leaflet package

  • Start with something like this:
    • Plotting the birthplace of Mahatma Gandhi.
library(leaflet)

# Set value for the minZoom and maxZoom settings.
#leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18))

myMap <- leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18)) %>% #This creates a map widget.
  addProviderTiles(providers$Esri.WorldImagery) %>%
  addMarkers(lng=69.629265, lat=21.641706, 
             popup="The birthplace of Mahatma Gandhi") %>%
  setView(lng=69.629265, lat=21.641706,zoom = 5)
  
myMap  # Print The Map

## See what "Providers\(" offers: * Play with `providers\)` and see what happens

myMap <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap)
myMap
myMap3 <- leaflet() %>%
addProviderTiles(providers$Stamen.Terrain)
myMap3
myMap4 <- leaflet() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap)
myMap4

Look! It’s Athens, GA

  • Use setview and see what happens:
myMap <- leaflet() %>%
  addProviderTiles(providers$OpenTopoMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap

Zoom in to spot Conner Hall

myMap <- leaflet() %>%
  addProviderTiles(providers$Esri.WorldImagery) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 20)%>%
  addMarkers(lat=33.947474, lng=-83.373671, 
             popup="Agricultural & Applied Economists Are Made Here")

myMap

Locating “Birthplace of Renaissance”: Florence, Italy

myMap <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(lat=43.769562, lng=11.255814, zoom = 12)
myMap

Misc: Interactive Map of The United States of America

  • Using library(maps) and leaflet to plot interactive United States Map:
if (!"maps" %in% installed.packages()) install.packages("maps")
library(maps)
library(leaflet)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
  addPolygons(fillColor = topo.colors(20, alpha = NULL), stroke = FALSE)

2. Add Your Own ShapeFiles

  • Locating Rohingya refugee camps:
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
refugeecamp <- read_sf("190310_Outline_Rohingya_Refugee_CampBlock_A2.shp")
  • Using the above data in Leaflet and transforming to get proper dimensions.
refugee_4326 <- refugeecamp %>%
  st_transform(4326)
head(refugee_4326)
## Simple feature collection with 6 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 92.13366 ymin: 21.2007 xmax: 92.13933 ymax: 21.21256
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 6 x 10
##   OBJECTID Block_No Block_ID Blck_Let CampName CampSSID Shape_Leng Shape_Area
##      <dbl> <chr>    <chr>    <chr>    <chr>    <chr>         <dbl>      <dbl>
## 1      159 163      CXB-232~ H        Camp 4 ~ CXB-232       1336.       7.11
## 2      160 157      CXB-232~ B        Camp 4 ~ CXB-232       1082.       8.01
## 3      161 161      CXB-232~ F        Camp 4 ~ CXB-232        761.       3.60
## 4      162 158      CXB-232~ C        Camp 4 ~ CXB-232       2511.      16.3 
## 5      163 160      CXB-232~ E        Camp 4 ~ CXB-232       1430.       7.05
## 6      164 162      CXB-232~ G        Camp 4 ~ CXB-232        715.       3.33
## # ... with 2 more variables: BlockNam <chr>, geometry <MULTIPOLYGON [°]>
leaflet() %>% 
  addProviderTiles(providers$Esri) %>% 
  setView(92.14871, 21.18780, zoom = 10) %>%
  addPolygons(data=refugee_4326, fill=TRUE, stroke=T, weight=1,
              highlight = highlightOptions(fillOpacity = 0.9),
              label = refugee_4326$Block_No)

3. Add tiles from the web

  • Start a new leaflet map and zoom onto the USA.
  • Use addWMSTiles() to add WMS (Web Map Service) tiles.
#require(maps)
#require(leaflet)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
  addPolygons(fillColor = topo.colors(20, alpha = NULL), stroke = FALSE) %>%
  addWMSTiles( 
    "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", 
    layers = "nexrad-n0r-900913", 
    options = WMSTileOptions(format = "image/png", transparent = TRUE), 
    attribution = "Weather data © 2012 IEM Nexrad" 
    )
  • Looking at a current WMS Layer from IOWA State University.
leaflet(data = mapStates) %>% addTiles() %>%
  addPolygons(fillColor = topo.colors(4, alpha = 0.5), stroke = FALSE) %>%
  addWMSTiles(
    "https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r-t.cgi?",
    layers = "nexrad-n0r-wmst", 
    options = WMSTileOptions(format = "image/png", transparent = TRUE), 
    attribution = "Weather data © 2020 IEM Nexrad") 

  1. Any further comments or sugggestions are appreciated and can be left in the comments tab in Rpubs at the bottom of the screen.↩︎