Show the code
pacman::p_load(tidyverse, tmap, sf)Goh Si Hui
February 26, 2024
February 26, 2024
In this exercise, we will learn how to: - convert an aspatial data file into simple point feature data frame, and assign it an appropriate projection reference to the newly created simple point feature data frame. - plot interactive proportional symbol maps.
We will be creating a proportional symbol map showing the number of Group1 and Group 2 wins by Singapore Pools’ outlets using an tmap package.
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.
For this exercise, other than tmap, we will use the following packages:
The code chunk below uses p_load() of pacman package to check if the abovementioned packages are installed in the computer. If they are, they will be launched in R. Otherwise, pacman will install the relevant packages before launching them.
We will be using the data set SGPools_svy21 for this exercise. The data is in csv file format.
The code chunk below uses read_csv() function of readr package to import SGPools_svy21.csv into R as a tibble data frame called 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
From the above output, we see that the sgpools dataset consists of seven columns. The XCOORD and YCOORD columns are the x-coordinates and y-coordinates of SingPools outlets and branches. They are in Singapore SVY21 Projected Coordinates System.
list() instead of glimpse().We will convert sgpools data frame into a simple feature data frame using st_as_sf() of sf package.
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]>
From the above output, we noticed that: - a new column called geometry has been added into the data frame. - sgppols_sf is in point feature class. - its epsg ID is 3414.
the coords = argument requires us to provide the column name of the x-coordinates first, followed by the column name of the y-coordinates.
the crs = argument requires us to provide the coordinates system in epsg format. From epsg.io, we know that EPSG: 3414 is Singapore SVY21 Projected Coordinate System.
tmap_mode("view")
tm_basemap("CartoDB.Positron") +
tm_shape(sgpools_sf) +
tm_bubbles(col = "red",
size = 1,
border.col = "black",
border.lwd = 1) +
tm_layout(title = "Locations of SGPools Branches and Outlets",
title.size = 1)To draw a proportional symbol map, we need to assign a numerical variable to the size visual attribute. The code chunks below show that the variable Gp1Gp2 Winnings is assigned to size visual attribute.
tm_basemap("CartoDB.Positron") +
tm_shape(sgpools_sf) +
tm_bubbles(col = "red",
size = "Gp1Gp2 Winnings",
border.col = "black",
border.lwd = 1) +
tm_layout(title = "Number of Gp1Gp2 Winnings",
title.size = 1)The proportional symbol map can be further improved using the colour visual attribute. In the code chunk below, OUTLET TYPE variable is used as the colour attribute variable, as such, Singapore Pools Branches and Outlets have different colour representation on the map.
We can also create interactive facted plots using tm_facets() to create multiple maps with synchonised zoom and pan settings.
tm_basemap("CartoDB.Positron") +
tm_shape(sgpools_sf) +
tm_bubbles(col = "OUTLET TYPE",
size = "Gp1Gp2 Winnings",
border.col = "black",
border.lwd = 1) +
tm_layout(title = c("Number of Gp1Gp2 Winnings by SgPools Branches", "Number of Gp1Gp2 Winnings by Outlets"),
title.size = 1) +
tm_facets(by = "OUTLET TYPE",
nrow = 1,
sync = TRUE)Let us switch back to plot mode to ensure that future visualisations are in plot mode.