Hands-on Exercise 7B

Author

Janet Toa

Published

February 26, 2024

Modified

February 26, 2024

22 Visualising Geospatial Point Data

22.1 Overview and Learning Outcomes

This hands-on exercise is based on Chapter 22 of the R for Visual Analytics book.

Proportional symbol maps (also known as graduate symbol maps) are a class of maps that use the visual variable of size to represent differences in the magnitude of a discrete, abruptly changing phenomenon, e.g. counts of people. Like choropleth maps, you can create classed or unclassed versions of these maps. The classed ones are known as range-graded or graduated symbols, and the unclassed are called proportional symbols, where the area of the symbols are proportional to the values of the attribute being mapped. In this hands-on exercise, you will learn how to create a proportional symbol map showing the number of wins by Singapore Pools’ outlets using an R package called tmap.

By the end of this hands-on exercise, you will acquire the following skills by using appropriate R packages:

  • 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.

22.2 Getting Started

22.2.1 Installing and Loading Required Libraries

In this hands-on exercise, the following R packages are used:

  • tidyverse (i.e. readr, tidyr, dplyr) for performing data science tasks such as importing, tidying, and wrangling data;

  • sf for handling geospatial data; and

  • tmap for thematic mapping.

The code chunk below uses the p_load() function in the pacman package to check if the packages are installed. If yes, they are then loaded into the R environment. If no, they are installed, then loaded into the R environment.

pacman::p_load(tidyverse, sf, tmap)

22.2.2 Importing and Preparing Data

The dataset for this hands-on exercise, SGPools_svy21, which is in the csv file format, is imported into the R environment using the read_csv() function in the readr package and stored as the R object, sgpools.

sgpools = read_csv("data/aspatial/SGPools_svy21.csv")

The tibble data frame, sgpools, has 7 columns and 306 rows. The “XCOORD” and “YCOORD” variables are the x-coordinates and y-coordinates of Singapore Pools outlets/branches. They are in the Singapore SVY21 Projected Coordinates System.

list(sgpools) 
[[1]]
# A tibble: 306 × 7
   NAME           ADDRESS POSTCODE XCOORD YCOORD `OUTLET TYPE` `Gp1Gp2 Winnings`
   <chr>          <chr>      <dbl>  <dbl>  <dbl> <chr>                     <dbl>
 1 Livewire (Mar… 2 Bayf…    18972 30842. 29599. Branch                        5
 2 Livewire (Res… 26 Sen…    98138 26704. 26526. Branch                       11
 3 SportsBuzz (K… Lotus …   738078 20118. 44888. Branch                        0
 4 SportsBuzz (P… 1 Sele…   188306 29777. 31382. Branch                       44
 5 Prime Serango… Blk 54…   552542 32239. 39519. Branch                        0
 6 Singapore Poo… 1A Woo…   731001 21012. 46987. Branch                        3
 7 Singapore Poo… Blk 64…   370064 33990. 34356. Branch                       17
 8 Singapore Poo… Blk 88…   370088 33847. 33976. Branch                       16
 9 Singapore Poo… Blk 30…   540308 33910. 41275. Branch                       21
10 Singapore Poo… Blk 20…   560202 29246. 38943. Branch                       25
# ℹ 296 more rows

The sgpools data frame is then converted into a simple feature data frame using the st_as_sf() function in the sf package.

The “coords” argument requires the column name of the x-coordinates first followed by the column name of the y-coordinates.

The “crs” argument requires the coordinates system in epsg format. EPSG: 3414 is the Singapore SVY21 Projected Coordinate System.

Figure below shows the data table of sgpools_sf. Notice that

sgpools_sf = st_as_sf(sgpools, 
                       coords = c("XCOORD", "YCOORD"),
                       crs= 3414)

A new column called geometry has been added into the sf data frame, sgpools_sf, which is a point feature class.

list(sgpools_sf)
[[1]]
Simple feature collection with 306 features and 5 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 7844.194 ymin: 26525.7 xmax: 45176.57 ymax: 47987.13
Projected CRS: SVY21 / Singapore TM
# A tibble: 306 × 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 Sen… 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 C… 1A Woo…   731001 Branch                        3
 7 Singapore Pools 64 Circuit … Blk 64…   370064 Branch                       17
 8 Singapore Pools 88 Circuit … Blk 88…   370088 Branch                       16
 9 Singapore Pools Anchorvale … Blk 30…   540308 Branch                       21
10 Singapore Pools Ang Mo Kio … Blk 20…   560202 Branch                       25
# ℹ 296 more rows
# ℹ 1 more variable: geometry <POINT [m]>

22.3 Drawing Proportional Symbol Map

The “view” mode is turned on via the tmap_mode() function to create an interactive proportional symbol map.

tmap_mode("view")

22.3.1 Starting with Interactive Point Symbol Map

An interactive point symbol map is created below to show where the Singapore Pools outlets/branches are.

tm_shape(sgpools_sf)+
tm_bubbles(col = "red",
           size = 1,
           border.col = "black",
           border.lwd = 1)

22.3.2 Making Map Proportional

A numerical variable is assigned to the “size” attribute to draw a proportional symbol map. The variable “Gp1Gp2Winnings” is assigned in the plot below.

tm_shape(sgpools_sf)+
tm_bubbles(col = "red",
           size = "Gp1Gp2 Winnings",
           border.col = "black",
           border.lwd = 1)

22.3.3 Using Different Colours

The proportional symbol map can be further improved by using the “col” attribute. The “OUTLET_TYPE variable is used as the colour attribute variable in the plot below.

tm_shape(sgpools_sf)+
tm_bubbles(col = "OUTLET TYPE", 
          size = "Gp1Gp2 Winnings",
          border.col = "black",
          border.lwd = 1)

22.3.4 Plotting Facet Plots

An impressive and little-know feature of tmap’s view mode is that it also works with faceted plots. The “sync” argument the in tm_facets() function 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)

Before ending the session, the tmap_mode() function is used to switch the mode back to “plot”.

tmap_mode("plot")

22.4 References

21.4.1 All About tmap Package

21.4.2 Geospatial Data Wrangling

21.4.3 General Data Wrangling

~~~ End of Hands-on Exercise 7B ~~~