Estimated Singapore Resident Population in Singapore in 2016

Any district that has population larger than 100K will be in red, else it will be in blue. The data is drawn using API from Data Gov.

## Getting data from Data Gov API (Estimated Singapore Resident Population in HDB Flats)
data <- GET(url = 'https://data.gov.sg', path= '/api/action/datastore_search?resource_id=b29c1af8-e11a-4e61-b813-933db9f69633&q=2016')

## Convert the content and parse from JSON to list
data <- fromJSON(rawToChar(data$content))

## Convert into tibble - dataframe,
## Select and filter only necessary columns and rows. 
## And, adding additional column indicating population above or below 100K using colours.
pop_df <- as_tibble(as.data.frame(data[["result"]]["records"]))
pop_df <- pop_df %>%
        select(records.town_or_estate, records.population) %>%
        rename(estate = records.town_or_estate, pop = records.population) %>%
        filter(estate != "Total") %>%
        mutate(pop=as.numeric(pop), col=ifelse(pop>100000, "red", "blue")) %>%
        arrange(estate)

And, the Singapore’s estates’ coordinates are extracted manually from Google Map.

## Adding Singapore's estates' coodinates from Google Map
estateLatLong <- data.frame(est = c(pop_df$estate),
  lat = c(1.381148, 1.327526, 1.354691, 1.354103, 1.277110, 1.358699, 
          1.329379, 1.291667, 1.384483, 1.323920, 1.321513, 1.364441, 1.330313,
          1.341228, 1.313998, 1.302475, 1.378757, 1.404279, 1.293249,
          1.452369, 1.393968, 1.362479, 1.346134, 1.336741, 1.441856, 1.415465),
  lng = c(103.844718, 103.923694, 103.839691, 103.755501, 103.819265, 103.775094,
           103.789478, 103.85, 103.750549, 103.759651, 103.888051, 103.889564, 103.736083,
           103.703178, 103.860187, 103.896764, 103.951289, 103.908010, 103.792790, 
           103.819158, 103.886634, 103.867214, 103.952967, 103.855900, 103.789122, 103.838902)
)

## Lastly, combine two tables together
estate <- bind_cols(pop_df, estateLatLong) %>%
        select(estate, pop, col, lat, lng)
estate
## # A tibble: 26 x 5
##    estate           pop col     lat   lng
##    <chr>          <dbl> <chr> <dbl> <dbl>
##  1 Ang Mo Kio    145700 red    1.38  104.
##  2 Bedok         196400 red    1.33  104.
##  3 Bishan         64400 blue   1.35  104.
##  4 Bukit Batok   111500 red    1.35  104.
##  5 Bukit Merah   144800 red    1.28  104.
##  6 Bukit Panjang 121700 red    1.36  104.
##  7 Bukit Timah     8400 blue   1.33  104.
##  8 Central Area   30800 blue   1.29  104.
##  9 Choa Chu Kang 167900 red    1.38  104.
## 10 Clementi       71900 blue   1.32  104.
## # ... with 16 more rows

Data Visualization

## Plotting the map with leaflet.
estate %>%
  leaflet(width = 900, height = 750) %>%
  addTiles() %>%
  addCircles(weight = 1, radius = sqrt(estate$pop)*5, color = estate$col, popup = estate$estate) %>%
  addLegend(labels = c("Above 100K population", "Below 100K population"), colors = c("red", "blue"))