Before we get started, we need to loaded some packages into R.
library(sf)
library(ggplot2)
library(ggspatial)
library(tmap)
library(tidyverse)
Load shapefile data
library(dplyr)
spBali <- st_read("BALI.shp")
## Reading layer `BALI' from data source
## `D:\2. Pengembangan diri\1 Exercise Bagus\Spasial in R\Choropleth Mapping\BALI.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 9 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 114.4316 ymin: -8.84919 xmax: 115.7125 ymax: -8.061396
## Geodetic CRS: WGS 84 (G730)
spBali
## Simple feature collection with 9 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 114.4316 ymin: -8.84919 xmax: 115.7125 ymax: -8.061396
## Geodetic CRS: WGS 84 (G730)
## fid idkab nmprov nmkab kdprov kdkab sumber periode
## 1 50992 5101 BALI JEMBRANA 51 01 BPS 2020_1
## 2 51043 5102 BALI TABANAN 51 02 BPS 2020_1
## 3 51176 5103 BALI BADUNG 51 03 BPS 2020_1
## 4 51238 5104 BALI GIANYAR 51 04 BPS 2020_1
## 5 51308 5105 BALI KLUNGKUNG 51 05 BPS 2020_1
## 6 51367 5106 BALI BANGLI 51 06 BPS 2020_1
## 7 51439 5107 BALI KARANGASEM 51 07 BPS 2020_1
## 8 51517 5108 BALI BULELENG 51 08 BPS 2020_1
## 9 51665 5171 BALI DENPASAR 51 71 BPS 2020_1
## geometry
## 1 MULTIPOLYGON (((114.4382 -8...
## 2 MULTIPOLYGON (((114.9252 -8...
## 3 MULTIPOLYGON (((115.1144 -8...
## 4 MULTIPOLYGON (((115.2277 -8...
## 5 MULTIPOLYGON (((115.3629 -8...
## 6 MULTIPOLYGON (((115.249 -8....
## 7 MULTIPOLYGON (((115.4126 -8...
## 8 MULTIPOLYGON (((114.5199 -8...
## 9 MULTIPOLYGON (((115.1777 -8...
AuxBali <- read_csv("Auxilary Bali.csv")
AuxBali <- AuxBali %>%
mutate(idkab = as.character(idkab)) %>%
mutate(Durability_Category = case_when(
Durability > 90 ~ "High Durable",
Durability <= 90 ~ "Low Durable"
))
Joining the attribute data and geospatial data
library(dplyr)
dataworking <- left_join(spBali, AuxBali, by = c("idkab"))
Plotting by using qtm()
qtm(dataworking, fill = "RLH", fill.title = "RLH", fill.palette = "Greens")
The default colour scheme used is “YlOrRd” of ColorBrewer
By default, missing value will be shaded in grey.
tm_shape(dataworking)+
tm_polygons("RLH")
tm_shape(dataworking)+
tm_fill("RLH")
tm_shape(dataworking)+
tm_fill("RLH") +
tm_borders(alpha = 0.5)
Data classification methods of tmap.
tm_shape(dataworking)+
tm_fill("RLH",
n = 5,
style = "quantile",
#breaks=c(0, 0.1, 0.3, 0.4, 0.8, 1)
) +
tm_borders(alpha = 0.5)
tm_shape(dataworking)+
tm_fill("RLH",
n = 5,
style = "equal") +
tm_borders(alpha = 0.5)
To change the colour, we assign the prefered colour to palette
argument of tm_fill()
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "Greens") +
tm_borders(alpha = 0.5)
To reverse the colour shading, add a “-” prefix.
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "-Greens") +
tm_borders(alpha = 0.5)
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "Blues",
legend.hist = TRUE,
legend.is.portrait = TRUE,
legend.hist.z = 0.1) +
tm_layout(legend.height = 0.45,
legend.width = 0.35,
legend.outside = FALSE,
legend.position = c("left", "bottom"),
frame = FALSE
) +
tm_borders(alpha = 0.5)
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "-Greens") +
tm_borders(alpha = 0.5) +
tmap_style("classic")
In tmap, several legend options are provided to change the placement, format and appearance of the legend.
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "Blues",
legend.hist = TRUE,
legend.is.portrait = TRUE,
legend.hist.z = 0.1) +
tm_layout(legend.height = 0.45,
legend.width = 0.35,
legend.outside = FALSE,
legend.position = c("left", "bottom"),
frame = FALSE
) +
tm_borders(alpha = 0.5)
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "-Greens") +
tm_borders(alpha = 0.5) +
tmap_style("classic")
tm_shape(dataworking)+
tm_fill("RLH",
style = "quantile",
palette = "Blues",
title = "No. of persons") +
tm_layout(main.title = "Housing Adequate \n by Susenas",
main.title.position = "center",
main.title.size = 1.2,
frame = TRUE) +
tm_borders(alpha = 0.5) +
tm_compass(type="8star", size = 2) +
tm_scale_bar(width = 0.15) +
tm_grid() +
tm_credits("Source: Susenas Maret 2024", position = c("left", "bottom"))
By assigning multiple values to at least one of the aesthetic arguments
tm_shape(dataworking)+
tm_fill(c("Sanit", "Suffi"),
style = "equal",
palette = "Blues") +
tm_layout(legend.position = c("left", "bottom")) +
tm_borders(alpha = 0.5) +
tmap_style("white")
tm_shape(dataworking)+
tm_polygons(c("RLH","RLH"),
style = c("equal", "quantile"),
palette = list("Blues","Greens")) +
tm_layout(legend.position = c("left", "bottom"))
By defining a group-by variable in tm_facets()
tm_shape(dataworking) +
tm_fill("RLH",
style = "quantile",
palette = "Blues") +
tm_facets(by="kabupaten",
free.coords=TRUE) +
tm_layout(legend.show = FALSE,
title.position = c("center", "center"),
title.size = 20) +
tm_borders(alpha = 0.5)
Multiple stand-alone maps with tmap_arrange()
youngmap <- tm_shape(dataworking)+
tm_polygons("DrinkingW",
style = "quantile",
palette = "Blues")
agedmap <- tm_shape(dataworking)+
tm_polygons("DrinkingW",
style = "quantile",
palette = "Greens")
tmap_arrange(youngmap, agedmap, asp=1.2, ncol=2)
tmap_mode("view")
tm_shape(dataworking)+
tm_fill("RLH",
n = 6,
style = "quantile",
palette = "Blues") +
tm_borders(alpha = 0.5)
Welfare Statistics Directorate, BPS, saptahas@bps.go.id