Hands-on Exercise 8B: Visualising Geospatial Point Data

Author

Kristine Joy Paas

Published

June 7, 2024

Modified

June 7, 2024

1 Overview

This hands-on exercise covers Chapter 22: Visualising Geospatial Point Data.

In this exercise, I learned:

  • To import an aspatial data file into R.

  • To convert it into simple point feature data frame and at the same time, to assign an appropriate projection reference to the newly create simple point feature data frame.

  • To plot interactive proportional symbol maps.

2 Getting Started

2.1 Loading the required packages

For this exercise we will use the following R packages:

  • sf: for processing geospatial data

  • tidyverse: data analytics tools for r

  • tmap: for generating maps

pacman::p_load(sf, tmap, tidyverse)

2.2 Importing data

The data set use for this hands-on exercise is called SGPools_svy21. The data is in csv file format.

sgpools <- read_csv("data/aspatial/SGPools_svy21.csv")
head(sgpools)
# A tibble: 6 × 7
  NAME            ADDRESS POSTCODE XCOORD YCOORD `OUTLET TYPE` `Gp1Gp2 Winnings`
  <chr>           <chr>      <dbl>  <dbl>  <dbl> <chr>                     <dbl>
1 Livewire (Mari… 2 Bayf…    18972 30842. 29599. Branch                        5
2 Livewire (Reso… 26 Sen…    98138 26704. 26526. Branch                       11
3 SportsBuzz (Kr… Lotus …   738078 20118. 44888. Branch                        0
4 SportsBuzz (Po… 1 Sele…   188306 29777. 31382. Branch                       44
5 Prime Serangoo… Blk 54…   552542 32239. 39519. Branch                        0
6 Singapore Pool… 1A Woo…   731001 21012. 46987. Branch                        3

It contains a list SG Pools outlets, and their corresponding geospatial data (XCOORD, YCOORD) that we can use to plot them in a map.

3 Data Wrangling

To work with sf we need to convert this data to the appropriate type:

sgpools_sf <- st_as_sf(sgpools, 
                       coords = c("XCOORD", "YCOORD"),
                       crs= 3414)
  • coords: specify which columns the coordinate information are in

  • crs: to provide the coordinates system in epsg format. EPSG: 3414 is Singapore SVY21 Projected Coordinate System. You can search for other country’s epsg code by refering to epsg.io.

head(sgpools_sf)
Simple feature collection with 6 features and 5 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 20117.93 ymin: 26525.7 xmax: 32238.69 ymax: 46987.32
Projected CRS: SVY21 / Singapore TM
# A tibble: 6 × 6
  NAME                          ADDRESS POSTCODE `OUTLET TYPE` `Gp1Gp2 Winnings`
  <chr>                         <chr>      <dbl> <chr>                     <dbl>
1 Livewire (Marina Bay Sands)   2 Bayf…    18972 Branch                        5
2 Livewire (Resorts World Sent… 26 Sen…    98138 Branch                       11
3 SportsBuzz (Kranji)           Lotus …   738078 Branch                        0
4 SportsBuzz (PoMo)             1 Sele…   188306 Branch                       44
5 Prime Serangoon North         Blk 54…   552542 Branch                        0
6 Singapore Pools Woodlands Ce… 1A Woo…   731001 Branch                        3
# ℹ 1 more variable: geometry <POINT [m]>

This contains a geometry column which contains the geospatial representation of the provided coordinates. In this case. there are point data.

4 Drawing Proportional Symbol Map

To create an interactive proportional symbol map in R, the view mode of tmap will be used.

The code churn below will turn on the interactive mode of tmap.

tmap_mode("view")

4.1 Plotting an interactive point symbol map

tm_shape(sgpools_sf)+
tm_bubbles(col = "red",
           size = 1,
           border.col = "black",
           border.lwd = 1)
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ

4.2 Proportional point sizes

The bubbles in the map above have the same sizes. Let’s make the bubbles bigger the higher the winnings are.

tm_shape(sgpools_sf)+
tm_bubbles(col = "red",
           size = "Gp1Gp2 Winnings",
           border.col = "black",
           border.lwd = 1)
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ

4.3 Assign colors

The proportional symbol map can be further improved by using the colour visual attribute. In the code chunks below, OUTLET_TYPE variable is used as the colour attribute variable.

tm_shape(sgpools_sf)+
tm_bubbles(col = "OUTLET TYPE", 
          size = "Gp1Gp2 Winnings",
          border.col = "black",
          border.lwd = 1)
OUTLET TYPE
Branch
Outlet
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ

4.4 Adding facets

The argument sync in tm_facets() can be used to produce multiple maps with synchronised zoom and pan settings.

tm_shape(sgpools_sf) +
  tm_bubbles(col = "OUTLET TYPE", 
          size = "Gp1Gp2 Winnings",
          border.col = "black",
          border.lwd = 1) +
  tm_facets(by= "OUTLET TYPE",
            nrow = 1,
            sync = TRUE)
Branch
OUTLET TYPE
Branch
Outlet
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ
Outlet
OUTLET TYPE
Branch
Outlet
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ

Switch back to non-interactive mode

tmap_mode("plot")

5 Reflections

During ISSS624, I rarely use interactive plots as it was using up a lot for resources. However, doing it the non-interactive way was more tedious as I had to do additional wrangling of the map use in the background (in this case, Singapore map).

However, the interactive map has a lot of features that I like (e.g. synchronized facets) so I’ll try to use it more.