class: center, middle, inverse, title-slide .title[ # Making interactive leaflet maps with R ] .subtitle[ ## Francisco RodrÃguez-Sánchez ] .author[ ###
@frod_san
] --- <style type="text/css"> .remark-slide-content { padding-top: 0px; } .remark-code { font-size: 13px; } .code10 .remark-code { font-size: 10%; } .code20 .remark-code { font-size: 20%; } .code30 .remark-code { font-size: 30%; } .code40 .remark-code { font-size: 40%; } .code50 .remark-code { font-size: 50%; } .code60 .remark-code { font-size: 60%; } .code70 .remark-code { font-size: 70%; } .code80 .remark-code { font-size: 80%; } .code90 .remark-code { font-size: 90%; } </style> --- class: inverse, center, middle # Mapping point data --- ## Palm trees in Sevilla <table> <thead> <tr> <th style="text-align:right;"> X </th> <th style="text-align:right;"> Y </th> <th style="text-align:left;"> species </th> <th style="text-align:right;"> perimeter </th> <th style="text-align:right;"> height </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> -5.972411 </td> <td style="text-align:right;"> 37.40811 </td> <td style="text-align:left;"> Washingtonia robusta </td> <td style="text-align:right;"> 119 </td> <td style="text-align:right;"> 12.0 </td> </tr> <tr> <td style="text-align:right;"> -5.951808 </td> <td style="text-align:right;"> 37.37690 </td> <td style="text-align:left;"> Phoenix canariensis </td> <td style="text-align:right;"> 240 </td> <td style="text-align:right;"> 5.0 </td> </tr> <tr> <td style="text-align:right;"> -5.987472 </td> <td style="text-align:right;"> 37.36899 </td> <td style="text-align:left;"> Phoenix dactylifera </td> <td style="text-align:right;"> 131 </td> <td style="text-align:right;"> 7.5 </td> </tr> <tr> <td style="text-align:right;"> -6.006355 </td> <td style="text-align:right;"> 37.37571 </td> <td style="text-align:left;"> Phoenix dactylifera </td> <td style="text-align:right;"> NA </td> <td style="text-align:right;"> NA </td> </tr> <tr> <td style="text-align:right;"> -5.973350 </td> <td style="text-align:right;"> 37.41542 </td> <td style="text-align:left;"> Washingtonia filifera </td> <td style="text-align:right;"> 108 </td> <td style="text-align:right;"> 8.0 </td> </tr> <tr> <td style="text-align:right;"> -6.006485 </td> <td style="text-align:right;"> 37.39652 </td> <td style="text-align:left;"> Chamaerops humilis </td> <td style="text-align:right;"> 45 </td> <td style="text-align:right;"> 2.0 </td> </tr> </tbody> </table> Source: https://opendata.esri.es/datasets/ideSEVILLA::parques-y-jardines-palmera-viario/about --- ## Make interactive map (leaflet) ```r library("leaflet") ``` https://rstudio.github.io/leaflet ([Cheatsheet](https://github.com/rstudio/cheatsheets/blob/main/leaflet.pdf)) --- ## Make interactive map (leaflet) ```r leaflet(palms) %>% addTiles() %>% addMarkers(lng = ~X, lat = ~Y) ```
--- ## Hey those are palm trees! ```r palm.icon <- makeIcon("https://img.icons8.com/ios-glyphs/30/000000/palm-tree.png") leaflet(palms) %>% addTiles() %>% addMarkers(lng = ~X, lat = ~Y, icon = palm.icon) ```
<a href="https://icons8.com/icon/104106/palm-tree">Palm Tree icon by Icons8</a> --- ## Make point clusters ```r leaflet(palms) %>% addTiles() %>% addMarkers(lng = ~X, lat = ~Y, icon = palm.icon, * clusterOptions = markerClusterOptions()) ```
--- ## Maybe just circles? ```r leaflet(palms) %>% addTiles() %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- class: inverse, center, middle # Changing basemaps --- ## Default tiles: OpenStreetMap ```r leaflet(palms) %>% * addTiles() %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- ## Using other tile providers ```r leaflet(palms) %>% * addProviderTiles(provider = providers$Esri.WorldImagery) %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- ## Using other tile providers ```r leaflet(palms) %>% * addProviderTiles(provider = providers$Stamen.Watercolor) %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- ## Using other tile providers ```r leaflet(palms) %>% * addProviderTiles(provider = providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- ## Using WMS tiles ```r leaflet(palms) %>% * addWMSTiles(baseUrl = "http://www.ign.es/wms-inspire/ign-base", * layers = "IGNBaseTodo-nofondo") %>% addCircleMarkers(lng = ~X, lat = ~Y, radius = 5, stroke = FALSE, fillOpacity = 0.7) ```
--- class: inverse, center, middle # Adding information to points --- ## Point size ~ palm height ```r leaflet(palms) %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, * radius = ~height) ```
--- ## Point colour ~ palm height ```r *pal <- colorNumeric(palette = "YlOrRd", domain = palms$height) leaflet(palms) %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 6, fillOpacity = 0.8, * color = ~pal(height)) ```
--- ## Add legend ```r leaflet(palms, height = '400px') %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 6, color = ~pal(height), fillOpacity = 0.7) %>% * addLegend(position = "bottomright", * pal = pal, values = ~height, opacity = 1) ```
--- ## Point colour ~ palm genus (factor) ```r *pal.gen <- colorFactor(palette = "Dark2", domain = palms$genus) leaflet(palms, height = '400px') %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 6, color = ~pal.gen(genus), fillOpacity = 0.7) %>% addLegend(position = "bottomright", pal = pal.gen, values = ~genus, opacity = 1) ```
--- ## Focus on particular area ```r leaflet(palms, height = '400px') %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 8, color = ~pal.gen(genus), fillOpacity = 0.7) %>% addLegend(position = "bottomright", pal = pal.gen, values = ~genus, opacity = 1) %>% * setView(lng = -6, lat = 37.38, zoom = 15) # see also fitBounds ```
--- ## Adding popups ```r leaflet(palms, height = '400px') %>% setView(lng = -6, lat = 37.38, zoom = 15) %>% addProviderTiles(provider = providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 8, color = ~pal.gen(genus), fillOpacity = 0.7, * popup = ~species) ```
--- class: inverse, middle, center # Advanced popups with `leafpop` https://github.com/r-spatial/leafpop --- ## Show tables with extra information ```r leaflet(palms, height = '400px') %>% setView(lng = -6, lat = 37.38, zoom = 15) %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 8, color = ~pal.gen(genus), fillOpacity = 0.7, * popup = ~leafpop::popupTable(palms, * zcol = c("species", "perimeter", "height"), * row.numbers = FALSE, feature.id = FALSE)) ```
--- ## Popups can show images, videos, anything! ```r leaflet(palms.redux) %>% addProviderTiles(providers$CartoDB.Positron) %>% addCircleMarkers(lng = ~X, lat = ~Y, stroke = FALSE, radius = 8, color = ~pal.gen(genus), fillOpacity = 0.7, * popup = ~leafpop::popupImage(img = palms.redux$pics, * src = "local", embed = TRUE)) ```
--- ## Popups can show images, videos, anything!