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 MapLook! It’s Athens, GA
- Use
setviewand see what happens:
Zoom in to spot Conner Hall
Locating “Birthplace of Renaissance”: Florence, Italy
Misc: Interactive Map of The United States of America
- Using
library(maps)and leaflet to plot interactive United States Map:
2. Add Your Own ShapeFiles
- Locating Rohingya refugee camps:
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
- Using the above data in Leaflet and transforming to get proper dimensions.
## 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 [°]>
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") References
Any further comments or sugggestions are appreciated and can be left in the comments tab in Rpubs at the bottom of the screen.↩︎