pacman::p_load(sf,tmap,tidyverse)In-class Exercise 3
3.1 Overview
3.2 Installing the Required Packages
Since maptools is retired and binary is removed from CRAN, we will be downloading it from the posit public package manager snapshots.
Note: It is important to add
eval:falsein the code chunk as shown below after installation is complete to avoid it being executed every time the quarto document is being rendered.
install.packages("maptools",repos = "https://packagemanager.posit.co/cran/2023-10-13")
3.3 The Data
mpsz_sf <- st_read(dsn="data/MasterPlan2014SubzoneBoundaryWebSHP/",
layer="MP14_SUBZONE_WEB_PL")Reading layer `MP14_SUBZONE_WEB_PL' from data source
`/Users/georgiaxng/georgiaxng/is415-handson/In-class_Ex/In-class_Ex03/data/MasterPlan2014SubzoneBoundaryWebSHP'
using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
The code chunk below, st_union() is used to derive the coastal outline sf tibble data.frame.
sg_sf <-mpsz_sf %>%
st_union()plot(sg_sf)
3.4 Viewing
The below chunk of code imports the ACLED Myanmar data, converts it into a spatial format, changes the coordinate system, and formats the event dates into a standard date format.
acled_sf <- read_csv("data/ACLED_Myanmar.csv") %>%
st_as_sf(coords = c(
"longitude", "latitude"), crs = 4326) %>%
st_transform(crs= 32647)%>%
mutate(event_date = dmy(event_date))This code produces an interactive map displaying dots for events in 2023 or classified as “Political violence.
tmap_mode('view')
acled_sf %>%
filter(year == 2023 |
event_type == "Political violence") %>%
tm_shape()+
tm_dots()
tmap_mode('plot')