Overview

You are tasked to use geospatial visualization tools (e.g. Leaflet) to create a choropleth map to illustrate how prices for a 4-Room HDB resale flat vary across different areas of Singapore (e.g. using Singapore Master Plan 2014 Planning Area Boundary). Using the map, provide a description of the possible association between HDB resale prices and distance to the CBD.

Loading Libraries

# Loading Libraries
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.3
## Loading required package: sp
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/chenl/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/chenl/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.4-1
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.6.3
library(htmlwidgets)
## Warning: package 'htmlwidgets' was built under R version 3.6.3

Loading and Cleaning Data

# Loading Data
setwd("/Users/chenl/Desktop/Question 1")
PAB_Data <- readOGR("MP14_PLNG_AREA_NO_SEA_PL.kml",
                    "MP14_PLNG_AREA_NO_SEA_PL",
                    encoding="utf-8")
## OGR data source with driver: KML 
## Source: "C:\Users\chenl\Desktop\Question 1\MP14_PLNG_AREA_NO_SEA_PL.kml", layer: "MP14_PLNG_AREA_NO_SEA_PL"
## with 55 features
## It has 2 fields
## Warning in readOGR("MP14_PLNG_AREA_NO_SEA_PL.kml", "MP14_PLNG_AREA_NO_SEA_PL", :
## Z-dimension discarded
RFP_1516 <- read.csv("./Avg-RFP-4Room-2015-2016.csv", header=TRUE)
RFP_17 <- read.csv("./Avg-RFP-4Room-2017.csv", header=TRUE)
head(RFP_1516)
head(RFP_17)

Plotting Polygons

# Plotting Polygons
plot(PAB_Data, col="#f2f2f2", bg="skyblue", lwd=0.25)

Formatting our Choropleth Map

# Formatting the Color Scheme
palette <- colorBin(c('#fff5f0', '#fee0d2', '#fcbba1', '#fc9272',
                      '#fb6a4a', '#ef3b2c', '#cb181d', '#a50f15', '#67000d'),
                    bins=c(0, 100000, 200000, 300000, 400000,
                           500000, 600000, 700000, 800000))

# Formatting Pop-Up Information Windows
popup1 <- paste0("<span style='color: #7f0000'>
                 <strong>Average 4-Room HDB Resale Flat Prices (2015 - 2016)</strong></span>",
                 "<br><span style='color: salmon;'><strong>Town: </strong></span>",
                 RFP_1516$town,
                 "<br><span style='color: salmon;'><strong>Average Resale Price: </strong></span>",
                 RFP_1516$avg_resale_price)
popup2 <- paste0("<span style='color: #7f0000'>
                 <strong>Average 4-Room HDB Resale Flat Prices (2017)</strong></span>",
                 "<br><span style='color: salmon;'><strong>Town: </strong></span>",
                 RFP_17$town,
                 "<br><span style='color: salmon;'><strong>Average Resale Price: </strong></span>",
                 RFP_17$avg_resale_price)

Plotting our Choropleth Map

# Plotting our Choropleth Map
mymap <- leaflet() %>%
  
  addProviderTiles("Esri.WorldGrayCanvas",
                   options=tileOptions(minZoom=10, maxZoom=16)) %>%
  
  addPolygons(data=PAB_Data,
              fillColor=~palette(RFP_1516$avg_resale_price),
              fillOpacity=0.6,
              color="darkgrey",
              weight=1.5,
              popup=popup1,
              group="<span style='color: #7f0000; font-size: 11pt'><strong>2015 - 2016</strong></span>") %>%
  
  addPolygons(data=PAB_Data,
              fillColor=~palette(RFP_17$avg_resale_price),
              fillOpacity=0.2,
              color="white",
              weight=2.0,
              popup=popup2,
              group="<span style='color: #7f0000; font-size: 11pt'><strong>2017 Onwards</strong></span>") %>%
  
  addLayersControl(
    baseGroups=c("<span style='color: #7f0000; font-size: 11pt'><strong>2015 - 2016</strong></span>",
                   "<span style='color: #7f0000; font-size: 11pt'><strong>2017 Onwards</strong></span>"),
    options=layersControlOptions(collapsed=FALSE)) %>%
  
  addLegend(position='topleft',
            colors=c('#fff5f0', '#fee0d2', '#fcbba1', '#fc9272',
                     '#fb6a4a', '#ef3b2c', '#cb181d', '#a50f15', '#67000d'),
            labels=c('0', '100K', '200K', '300K', '400K', '500K', '600K', '700K', '800K'),
            opacity=0.6,
            title="Average<br>Resale<br>Price")

mymap
saveWidget(mymap, file = "mymap.html", selfcontained = FALSE)

Conclusion

Our Choropleth Map illustrates the variation in average prices for a 4-Room HDB Resale Flat across different areas of Singapore. We included 2 different time frames from 2015 to 2016, and from 2017 onwards. In summary, the average price for a 4-Room HDB Resale Flat increased as the distance from its location to the CBD decreased. This suggests a possible negative association between HDB resale prices and distance to the CBD.

Please refer to the following link on RPubs for the published Widget: https://rpubs.com/chenlianghe Thank you very much!