9 Maps

9.1 Leaflet

To create interactive maps you need the leaflet library. This provides various functions that will be transalated into Javascript.

9.1.1 Display a basic map

## Warning: package 'leaflet' was built under R version 3.4.4

9.1.2 Add markers, shapes and popups to a map

9.1.3 Add Boundaries

To add boundaries you need geojson or topojson. This needs to be converted into a format the R understands using functions in the geojsonio library.

R converts this geojson into a Spatial Polygon Dataframe (SPD). This should consist of 2 main attributes: data which has descriptive information about the polygon such as a name, and polygons and contains all the coordinates for each polygon. To see what the contain you can us the following:

The boundary data can now be attached to the map when it is first initialized.

9.1.4 Add Interactivity

Highlights can be added when you hover over an area.

Popup info can also be added when you hover over an area. This requires using the sprintf function, which includes HTML.

The code that is written in the sprintf function rquires a special format. The first argument defines what and how text willl be dispalayed. It includes placeholders (eg. %s) that can have differnet types. All subsequent arguments represent the data variables that will are represented by the placeholders (in the order they occur).For more info see https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/sprintf.

9.1.5 Attach extra information to your boundary dataset

You will usually want to add additional data/statistics to the boundary file, so that you can improve visualition/interactivity. You can merge a regular dataframe wih an SPD provided they have a common variable (eg. ONS Id’s).