Interactive maps

This uses the same data from the mapping practical - airbnbs and hotels (OSM) in London.

## Reading layer `gis_osm_pois_free_1' from data source 
##   `C:\Users\Andy\OneDrive - University College London\Teaching\CASA0005\CASA0005repo\prac5_data\greater-london-latest-free.shp\gis_osm_pois_free_1.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 57306 features and 4 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -0.5090921 ymin: 51.29201 xmax: 0.2957296 ymax: 51.68432
## Geodetic CRS:  WGS 84
## Reading layer `London_Borough_Excluding_MHW' from data source 
##   `C:\Users\Andy\OneDrive - University College London\Teaching\CASA0005\CASA0005repo\prac1_data\statistical-gis-boundaries-london\ESRI\London_Borough_Excluding_MHW.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 33 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 503568.2 ymin: 155850.8 xmax: 561957.5 ymax: 200933.9
## Projected CRS: OSGB36 / British National Grid

To make our map interactive we change tmap_mode() from plot to view..

tmap_mode("view")

tm_shape(Accomodation_contained) + 
  tm_polygons("hotels_n") 

8.33 Advanced interactive map

But let’s take it a bit further so we can select our layers on an interactive map..

Set up some pop up box information..so if we click borough we can a small table.

#library for the pop out tables
library(leafpop)

#remove the geometry for our pop up boxes to avoid
popupairbnb <- Accomodation_contained %>%
  st_drop_geometry()%>%
  dplyr::select(airbnbs_n, NAME)%>%
  popupTable()

popuphotel <- Accomodation_contained %>%
  st_drop_geometry()%>%
  dplyr::select(hotels_n, NAME)%>%
  popupTable()

Let’s set a few more things up:

  • We are going to use Leaflet which requires the CRS to be in WGS84
  • We need to define our colours based on the breaks we previously set
# WGS84
Accomodation_containedWGS84 <-Accomodation_contained%>%
  st_transform(., 4326)

# leaflet library for interactive map
library(leaflet)

# set the colour palettes or breaks

pal1 <- colorBin("YlOrRd", 
                 domain = Accomodation_contained$airbnbs_n, 
                 bins = breaks$brks)
pal2 <- colorBin("Blues", 
                 domain = Accomodation_contained$hotels_n,
                 bins = breaks$brks)

Similar to our static maps we must:

  • First define the shape object
  • The load the map layer (e.g. polygons)

Here, we also set a group which links the polygon both the legend and button selection options.

# Build the map
map <- leaflet(Accomodation_containedWGS84) %>%
  
  # Airbnb layer
  addPolygons(color = "white", 
              weight = 2,
              opacity = 1,
              dashArray = "3",
              fillOpacity = 0.7,
              fillColor = ~pal1(airbnbs_n),
              popup = popupairbnb,
              group = "Airbnb") %>%

  # Hotel layer
  addPolygons(fillColor = ~pal2(hotels_n),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.7,
              popup = popuphotel,
              group = "Hotels") %>%

  
  # Add layer control
  addLayersControl(
    # names to give the button selection options
    baseGroups = c("OSM", "Toner", "Toner Lite", "CartoDB"),
    overlayGroups = c("Airbnb", "Hotels"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%

  # Airbnb legend (tied to Airbnb group)
  addLegend("bottomleft", 
            pal = pal1, 
            values = ~airbnbs_n,
            title = "Airbnb count",
            group = "Airbnb") %>%

  # Hotel legend (tied to Hotels group)
  addLegend("bottomleft", 
            pal = pal2, 
            values = ~hotels_n,
            title = "Hotel count",
            group = "Hotels")%>%
  
  #add basemaps
  addTiles(group = "OSM (default)") %>%
  addProviderTiles(providers$Stadia.StamenToner, group = "Toner") %>%
  addProviderTiles(providers$Stadia.StamenTonerLite, group = "Toner Lite")%>%
  addProviderTiles(providers$CartoDB.Positron, group = "CartoDB")

map

If you want to explore Leaflet more have a look at the leaflet for R Guide

To see other basemap options (there are loads!) have a look here at leaflet extras

Cosh, Georgie. 2020. GLA Housing and Land Short-term and holiday letting in London.”
Donoho, David. 2017. 50 Years of Data Science.” Journal of Computational and Graphical Statistics 26 (4): 745–66. https://doi.org/10.1080/10618600.2017.1384734.
Glassdoor. 2020. 50 Best Jobs in America for 2019.” https://www.glassdoor.co.uk/List/Best-Jobs-in-America-LST_KQ0,20.htm.
Grolemund, Garrett., and Hadley. Wickham. 2017. R for Data Science. O’Reilly.
Guha, Subhanil, Himanshu Govil, Anindita Dey, and Neetu Gill. 2018. Analytical study of land surface temperature with NDVI and NDBI using Landsat 8 OLI and TIRS data in Florence and Naples city, Italy.” European Journal of Remote Sensing 51 (1): 667–78. https://doi.org/10.1080/22797254.2018.1474494.
Li, Songnian, Suzana Dragicevic, Francesc Antón Castro, Monika Sester, Stephan Winter, Arzu Coltekin, Christopher Pettit, et al. 2016. Geospatial big data handling theory and methods: A review and research challenges.” ISPRS Journal of Photogrammetry and Remote Sensing 115: 119–33. https://doi.org/10.1016/j.isprsjprs.2015.10.012.
Lovelace, Robin., Jakub. Nowosad, and Jannes. Muenchow. 2019. Geocomputation with R. CRC Press. https://doi.org/10.1201/9780203730058.
Pattabiraman, Kumaresh. 2019. LinkedIn’s Most Promising Jobs of 2019.” https://blog.linkedin.com/2019/january/10/linkedins-most-promising-jobs-of-2019.
The Universities and Colleges Admissions Service. 2020. UCAS Postgraduate courses.” https://digital.ucas.com/coursedisplay/results/providers?studyYear=2019{\&}destination=Postgraduate{\&}postcodeDistanceSystem=imperial{\&}pageNumber=1{\&}sort=MostRelevant{\&}searchTerm=data science.