Data acquisition

req <- 'https://data.gov.sg/dataset/7a339d20-3c57-4b11-a695-9348adfd7614/resource/42ff9cfe-abe5-4b54-beda-c88f9bb438ee/data?limit=6000'

df <- fromJSON(req)$records %>% 
  as_tibble() %>% 
  mutate(town = str_to_title(town), 
         flat_type = str_to_title(flat_type),
         floor_area_sqm = as.numeric(floor_area_sqm),
         street_name  = str_to_title(street_name),
         resale_price = as.numeric(resale_price), 
         lease_commence_date = as.numeric(lease_commence_date), 
         storey_range = str_to_lower(storey_range), 
         block = paste0("Blk", block)) %>% 
  separate(month, c("year", "month"), sep = "-") %>% 
  mutate(year = as.numeric(year),
         month = as.numeric(month), 
         desc = paste0(town, "|", block, "|", storey_range, "|", 
                       flat_model, "|", flat_type, "|", floor_area_sqm, ":Mth", month)) %>% 
  select(-12)
df_table <- df %>% 
  select(-c("flat_type", "flat_model", "floor_area_sqm", "lease_commence_date", "storey_range", "block"))

tx <- df_table %>% 
  highlight_key()

dt <- DT::datatable(tx, rownames = FALSE, filter = "top",
                    caption = 'Table 1: HDB Transactions Prices from June/2019 - August/2019', 
                    extensions = 'Buttons', 
                    options = list(dom = 'Bfrtip',
                                   buttons = list('copy', 'print', 
                                                  list(extend = 'collection',
                                                       buttons = c('csv', 'excel', 'pdf'),
                                                       text = 'Download')))) %>% 
  DT::formatStyle(columns = c(1:13), fontSize = "8pt")

widgets <- bscols(
  widths = c(12),
  filter_checkbox("town", "Town", tx, ~town, inline = FALSE, columns = 1))

crosstalk::bscols(widgets, 
                  dt, 
                  widths = c(2, 10))   

Overivew

area <- c('Bukit Batok', 'Clementi', 'Jurong East', 'Jurong West')
room <- c('3 Room', '4 Room', '5 Room')
month <- c(6, 7, 8)

clemnti <- df %>% 
  filter(town == 'Clementi',
         flat_type %in% room,
         month %in% month, 
         lease_commence_date >= 2000)

rest <- df %>% 
  filter(town != 'Clementi',
         flat_type %in% room,
         month %in% month, 
         lease_commence_date >= 2000)


overview <- rest %>% 
  ggplot() +
  geom_jitter(aes(as.factor(lease_commence_date), resale_price, text = desc), 
              rest,
              alpha = 0.5) +
  geom_jitter(aes(as.factor(lease_commence_date), resale_price, text = desc), 
              clemnti,
              color = 'coral', 
              show.legend  = FALSE) +
  geom_hline(yintercept = 450000, color = 'red') +
  scale_y_continuous(labels = scales::comma_format()) +
  labs(title = 'HDB Transaction Prices (3|4|5 Rooms Built After 2000)', 
       subtitle = "June/2019 - August/2019", 
       x = '',
       y = '') +
  theme_light() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
 
ggplotly(overview)
20002001200220032004200520062007200820092010201120122013201420152016250,000500,000750,0001,000,0001,250,000
HDB Transaction Prices (3|4|5 Rooms Built After 2000)

Comparison

df %>% 
  filter(town == "Clementi", flat_type %in% c('3 Room', '4 Room')) %>% 
  mutate(block = fct_reorder(block, resale_price), 
         mth = paste0("Month: ", month)) %>% 
  ggplot(aes(resale_price, block, color = flat_type)) +
  geom_point() +
  geom_text(aes(label = lease_commence_date), check_overlap = TRUE, hjust = 0, vjust = 0, size = 3) +
  geom_vline(xintercept = 450000, color = 'red') +
  scale_x_continuous(labels = scales::comma_format()) +
  facet_wrap(~ mth, scales = 'free_y') +
  labs(title = 'HDB Transactions Prices',
       subtitle = 'Clementi from June/2019 - August/2019',
       x = '',
       y = '') +
  theme_light() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.title = element_blank())

df %>% 
  filter(town == "Jurong West", flat_type %in% c('3 Room', '4 Room')) %>% 
  mutate(block = fct_reorder(block, resale_price), 
         mth = paste0("Month: ", month)) %>% 
  ggplot(aes(resale_price, block, color = flat_type)) +
  geom_point() +
  geom_text(aes(label = lease_commence_date), check_overlap = TRUE, hjust = 0, vjust = 0, size = 3) +
  geom_vline(xintercept = 450000, color = 'red') +
  scale_x_continuous(labels = scales::comma_format()) +
  facet_wrap(~ mth, scales = 'free_y') +
  labs(title = 'HDB Transactions Prices',
       subtitle = 'Jurong West from June/2019 - August/2019',
       x = '',
       y = '') +
  theme_light() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.title = element_blank())

df %>% 
  filter(town %in% c('Clementi', 'Jurong West'), flat_type == '3 Room') %>% 
  mutate(block = fct_reorder(block, resale_price)) %>% 
  ggplot(aes(resale_price, block)) +
  geom_point() +
  geom_text(aes(label = lease_commence_date), check_overlap = TRUE, hjust = 1, vjust = 1, size = 3) +
  geom_vline(xintercept = 450000, color = 'red') +
  scale_x_continuous(labels = scales::comma_format()) +
  facet_wrap(~ town, scales = 'free_y') +
  labs(title = 'HDB Transactions Prices (3 Room)',
       subtitle = 'Clementi vs Jurong West from June/2019 - August/2019', 
       x = '', 
       y ='') +
  theme_light()