10  Interactive maps

You can plot a static map using plof(sf), but you can also create interactive maps.

library(sf)
TRIPSgeo = st_read("../data/TRIPSgeo.gpkg")
Reading layer `TRIPSgeo' from data source 
  `/media/rosa/Dados/GIS/MQAT/data/TRIPSgeo.gpkg' using driver `GPKG'
Simple feature collection with 18 features and 7 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -9.500527 ymin: 38.40907 xmax: -8.490972 ymax: 39.06472
Geodetic CRS:  WGS 84
plot(TRIPSgeo) # all variables

# one variable at a time
plot(TRIPSgeo["Municipality"])

plot(TRIPSgeo["Total"])

plot(TRIPSgeo["Car"])

Interactive maps are useful to explore the data, as you can zoom in and out, and click on the points to see the data associated with them.

There are several R packages to create interactive maps. For instance, the tmap package, the leaflet package, and the mapview package.

10.1 Mapview

Mapview allows to create quick interactive maps, only by declaring the function mapview().

library(mapview)
mapview(TRIPSgeo)

To color the points by a variable, you can use the zcol argument.

mapview(TRIPSgeo, zcol = "Total")

As you can see, a color palette is automatically assigned to the continuous variable.

Try to use a categorical variable.

Code
mapview(TRIPSgeo,
        zcol = "Municipality", # depending on the variable
        alpha.regions = 0.4, # also add transparency
        color = "white" # border color
        ) 

Note that you can change the basemap, and click on the geometries to see the data associated with them.

You can go crazy with all the options that mapview offers. Please refer to the documentation to see all the options.

Export

You can directly export the map as an html file or image, using the Viewer panel.

This is the most straightforward solution.

You can also export a map as an html file or image using code.

# install.packages("webshot2") # you will need this

map = mapview(TRIPSgeo, zcol = "Total") # first create an objet with the desired map

mapshot2(map, "data/map.html") # as webpage
mapshot2(map, file = "data/map.png") # as image

10.2 Rmarkdown

To include a map on a report, website, paper (any type), you can create an Rmarkdown file.

And include a R code chunk (ctrl + alt + i) with a map. If the output is html, you will get an interactive map on your document!

10.3 Flowmap blue

A way to visualize traffic or travel volumes between areas (OD pairs) is through the online tool flowmap.blue.

But you need to prepare the data so that they are exactly in the requested format. (See the used source code).

You need two files:

  1. locations
  2. flows

Try to copy and paste the content of these files in the corresponding fields the information about car trips in the Lisbon metropolitan area according to INE (2018).

Try it yourself

Visualize the public transit trips in the Lisbon metropolitan area. (flows_tp)

  • Which differences do you see between car and public transit trips?

  • How many trips are made daily in public transit with origin or destination in Sintra?