packages = c('sf', 'tmap', 'tidyverse', 'plotly', 'highcharter', 'dplyr')
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
## Loading required package: sf
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
## Loading required package: tmap
## Loading required package: tidyverse
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Loading required package: plotly
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
## Loading required package: highcharter
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
resale <- read.csv('resale price.csv', header = T)
subzone <- st_read("geographical data/MP14_SUBZONE_WEB_PL.shp")
## Reading layer `MP14_SUBZONE_WEB_PL' from data source `C:\Users\ASUS\Desktop\Y3S1\428 visual analytics\assignment 5\geographical data\MP14_SUBZONE_WEB_PL.shp' using driver `ESRI Shapefile'
## Simple feature collection with 323 features and 15 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
## projected CRS: SVY21
head(resale) #we can see that the columns month contains year data - we want to extract this
## month town flat_type block street_name storey_range
## 1 2017-01 ANG MO KIO 2 ROOM 406 ANG MO KIO AVE 10 10 TO 12
## 2 2017-01 ANG MO KIO 3 ROOM 108 ANG MO KIO AVE 4 01 TO 03
## 3 2017-01 ANG MO KIO 3 ROOM 602 ANG MO KIO AVE 5 01 TO 03
## 4 2017-01 ANG MO KIO 3 ROOM 465 ANG MO KIO AVE 10 04 TO 06
## 5 2017-01 ANG MO KIO 3 ROOM 601 ANG MO KIO AVE 5 01 TO 03
## 6 2017-01 ANG MO KIO 3 ROOM 150 ANG MO KIO AVE 5 01 TO 03
## floor_area_sqm flat_model lease_commence_date remaining_lease
## 1 44 Improved 1979 61 years 04 months
## 2 67 New Generation 1978 60 years 07 months
## 3 67 New Generation 1980 62 years 05 months
## 4 68 New Generation 1980 62 years 01 month
## 5 67 New Generation 1980 62 years 05 months
## 6 68 New Generation 1981 63 years
## resale_price
## 1 232000
## 2 250000
## 3 262000
## 4 265000
## 5 265000
## 6 275000
resale_modified <- separate(resale, month, c("Year", "Month"))
head(resale_modified)
## Year Month town flat_type block street_name storey_range
## 1 2017 01 ANG MO KIO 2 ROOM 406 ANG MO KIO AVE 10 10 TO 12
## 2 2017 01 ANG MO KIO 3 ROOM 108 ANG MO KIO AVE 4 01 TO 03
## 3 2017 01 ANG MO KIO 3 ROOM 602 ANG MO KIO AVE 5 01 TO 03
## 4 2017 01 ANG MO KIO 3 ROOM 465 ANG MO KIO AVE 10 04 TO 06
## 5 2017 01 ANG MO KIO 3 ROOM 601 ANG MO KIO AVE 5 01 TO 03
## 6 2017 01 ANG MO KIO 3 ROOM 150 ANG MO KIO AVE 5 01 TO 03
## floor_area_sqm flat_model lease_commence_date remaining_lease
## 1 44 Improved 1979 61 years 04 months
## 2 67 New Generation 1978 60 years 07 months
## 3 67 New Generation 1980 62 years 05 months
## 4 68 New Generation 1980 62 years 01 month
## 5 67 New Generation 1980 62 years 05 months
## 6 68 New Generation 1981 63 years
## resale_price
## 1 232000
## 2 250000
## 3 262000
## 4 265000
## 5 265000
## 6 275000
plot_by_storeys<- resale19 %>%
group_by(town, storey_range) %>%
summarise(mean_resale_price = mean(resale_price))
## `summarise()` regrouping output by 'town' (override with `.groups` argument)
plot_by_storeys <- plot_by_storeys %>%
mutate(text = paste0("Location: ", town, "\n", "Mean resale Price:", round(mean_resale_price,2)))
heatmap_by_storeys <- ggplot(data = plot_by_storeys,
mapping = aes(x = town, y = storey_range, fill = mean_resale_price, text=text)) +
geom_tile() +
labs(title = "Heatmap of Average HDB Resale Price by Area and Housing Storey (2019)", x = "Town", y = "Storey") +
scale_fill_gradient(name = "Mean Resale Price",
low = "#ffedde",
high = "#c24b0c")+
theme(axis.text.x = element_text(angle = 45))
ggplotly(heatmap_by_storeys, tooltip="text")
mean_prices <- resale19 %>%
group_by(town) %>%
summarise(mean_resale_price_2 = mean(resale_price))
## `summarise()` ungrouping output (override with `.groups` argument)
subzone_resale2019 <- left_join(subzone, mean_prices, by = c("PLN_AREA_N" = "town"))
tmap_mode("view")
## tmap mode set to interactive viewing
head(subzone_resale2019)
## Simple feature collection with 6 features and 16 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 24468.89 ymin: 28369.47 xmax: 32362.39 ymax: 30542.74
## projected CRS: SVY21
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N
## 1 1 1 MARINA SOUTH MSSZ01 Y MARINA SOUTH
## 2 2 1 PEARL'S HILL OTSZ01 Y OUTRAM
## 3 3 3 BOAT QUAY SRSZ03 Y SINGAPORE RIVER
## 4 4 8 HENDERSON HILL BMSZ08 N BUKIT MERAH
## 5 5 3 REDHILL BMSZ03 N BUKIT MERAH
## 6 6 7 ALEXANDRA HILL BMSZ07 N BUKIT MERAH
## PLN_AREA_C REGION_N REGION_C INC_CRC FMEL_UPD_D X_ADDR
## 1 MS CENTRAL REGION CR 5ED7EB253F99252E 2014-12-05 31595.84
## 2 OT CENTRAL REGION CR 8C7149B9EB32EEFC 2014-12-05 28679.06
## 3 SR CENTRAL REGION CR C35FEFF02B13E0E5 2014-12-05 29654.96
## 4 BM CENTRAL REGION CR 3775D82C5DDBEFBD 2014-12-05 26782.83
## 5 BM CENTRAL REGION CR 85D9ABEF0A40678F 2014-12-05 26201.96
## 6 BM CENTRAL REGION CR 9D286521EF5E3B59 2014-12-05 25358.82
## Y_ADDR SHAPE_Leng SHAPE_Area mean_resale_price_2
## 1 29220.19 5267.381 1630379.3 NA
## 2 29782.05 3506.107 559816.2 NA
## 3 29974.66 1740.926 160807.5 NA
## 4 29933.77 3313.625 595428.9 559507.2
## 5 30005.70 2825.594 387429.4 559507.2
## 6 29991.38 4428.913 1030378.8 559507.2
## geometry
## 1 MULTIPOLYGON (((31495.56 30...
## 2 MULTIPOLYGON (((29092.28 30...
## 3 MULTIPOLYGON (((29932.33 29...
## 4 MULTIPOLYGON (((27131.28 30...
## 5 MULTIPOLYGON (((26451.03 30...
## 6 MULTIPOLYGON (((25899.7 297...
complete_map <- tm_shape(subzone_resale2019)+
tm_fill("mean_resale_price_2",
n = 6,
style = "quantile",
palette = "Oranges",
id = 'SUBZONE_N',
title="Mean HDB Resale Price (2019)") +
tm_borders(alpha = 0.5)+
tm_polygons(popup.vars=c(
"Mean Resale Price: " = "mean_resale_price_2"))
tmap_options(limits=c(facets.plot=32, facets.view=5))
map_by_regions <- tm_shape(subzone_resale2019)+
tm_fill("mean_resale_price_2",
n = 6,
style = "quantile",
palette = "Oranges",
id = 'SUBZONE_N',
title=c("Central Region", "East Region", "North-East Region", "North Region", "West Region")) +
tm_borders(alpha = 0.5)+
tm_facets(by="REGION_N", free.coords=TRUE)+
tm_layout(legend.show = FALSE) +
tm_borders(alpha = 0.5)+
tm_polygons(popup.vars=c(
"Mean Resale Price: " = "mean_resale_price_2"))
ggplotly(heatmap_by_storeys, tooltip="text")
complete_map
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
map_by_regions
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).