Interactive maps

This uses the same data from the mapping practical - airbnbs and hotels (OSM) in London. Run all of that code before starting this!

## 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..Let’s set a few more things up:

  • Our CRS needs to be in WGS84
Accomodation_containedWGS84<- Accomodation_contained %>%
  st_transform(., 4326)

Similar to our static maps we must:

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

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

tmap_mode("view")

# Build the map

  # Airbnb layer
map <-  tm_shape(Accomodation_containedWGS84) +
        tm_polygons(fill="airbnbs_n",
                    fill.scale = list(tm_scale_intervals(values = "brewer.blues", breaks=breaks$brks)),
                    fill_alpha=0.5,
                    col="white",
                    lwd=2,
                    lty="dashed",
                    # select columns for popup
                    popup.vars=c("airbnbs_n", "NAME"),
                    group="Airbnb") + 
        
  # hotel layer  
        tm_polygons(fill="hotels_n",
                    fill.scale = list(tm_scale_intervals(values = "brewer.blues", breaks=breaks$brks)),
                    fill_alpha=0.5,
                    col="white",
                    lwd=2,
                    lty="dashed",
                    popup.vars=c("hotels_n", "NAME"),
                    group="Hotels")+
    # basemap
          tm_basemap(server = c("OpenStreetMap", "Thunderforest.Landscape"))
        

map

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