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)
pop_df %>%
plot_ly(x = ~estate, y = ~pop, type = "bar") %>%
layout(title = "Estimated SG Population in Singapore (2016)",
xaxis = list(title="Singapore Estates", tickangle=315),
yaxis = list(title ="Populations"))
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)
kable(estate)
|estate | pop|col | lat| lng|
|:---------------|------:|:----|--------:|--------:|
|Ang Mo Kio | 145700|red | 1.381148| 103.8447|
|Bedok | 196400|red | 1.327526| 103.9237|
|Bishan | 64400|blue | 1.354691| 103.8397|
|Bukit Batok | 111500|red | 1.354103| 103.7555|
|Bukit Merah | 144800|red | 1.277110| 103.8193|
|Bukit Panjang | 121700|red | 1.358699| 103.7751|
|Bukit Timah | 8400|blue | 1.329379| 103.7895|
|Central Area | 30800|blue | 1.291667| 103.8500|
|Choa Chu Kang | 167900|red | 1.384483| 103.7505|
|Clementi | 71900|blue | 1.323920| 103.7597|
|Geylang | 90200|blue | 1.321513| 103.8881|
|Hougang | 180500|red | 1.364441| 103.8896|
|Jurong East | 79600|blue | 1.330313| 103.7361|
|Jurong West | 260600|red | 1.341228| 103.7032|
|Kallang/Whampoa | 106500|red | 1.313998| 103.8602|
|Marine Parade | 22300|blue | 1.302475| 103.8968|
|Pasir Ris | 110200|red | 1.378757| 103.9513|
|Punggol | 126300|red | 1.404279| 103.9080|
|Queenstown | 82800|blue | 1.293249| 103.7928|
|Sembawang | 72500|blue | 1.452369| 103.8192|
|Sengkang | 207900|red | 1.393968| 103.8866|
|Serangoon | 70100|blue | 1.362479| 103.8672|
|Tampines | 234600|red | 1.346134| 103.9530|
|Toa Payoh | 107100|red | 1.336741| 103.8559|
|Woodlands | 241900|red | 1.441856| 103.7891|
|Yishun | 193300|red | 1.415465| 103.8389|
## Plotting the map with leaflet.
estate %>%
leaflet(width = 900, height = 750) %>%
addTiles() %>%
addCircles(weight = 1, radius = sqrt(estate$pop)*5, color = estate$col, popup = paste(estate$estate, estate$pop, sep = "<br />")) %>%
addLegend(labels = c("Above 100K population", "Below 100K population"), colors = c("red", "blue"))