Created a web page using R Markdown that features a map showing major population centres in Malaysia created with the Leaflet package. Hosted the webpage on either RPubs.
The webpage must contain the date the document was created, and it must contain a map created with Leaflet.
An attempt at Creativity was made!
library(dplyr)
library(leaflet)
library(maps)
library(tidyr)
library(RColorBrewer)
data("world.cities") # Using world.cities data set from library(maps)
str(world.cities)
'data.frame': 43645 obs. of 6 variables:
$ name : chr "'Abasan al-Jadidah" "'Abasan al-Kabirah" "'Abdul Hakim" "'Abdullah-as-Salam" ...
$ country.etc: chr "Palestine" "Palestine" "Pakistan" "Kuwait" ...
$ pop : int 5629 18999 47788 21817 2456 3434 9198 5492 22706 41731 ...
$ lat : num 31.3 31.3 30.6 29.4 32 ...
$ long : num 34.3 34.4 72.1 48 35.1 ...
$ capital : int 0 0 0 0 0 0 0 0 0 0 ...
Examining the structure of world.cities the variables of interest are from columns 1 & 3:5.
Reducing the dataset and filtering for population centres >100,000
Malaysia <- world.cities %>%
filter(country.etc == "Malaysia" & pop > 100000) %>%
select(-c(capital,country.etc))
Malaysia
name pop lat long
1 Alor Setar 222711 6.11 100.37
2 Ampang Jaya 682053 3.15 101.77
3 Ayer Itam 118901 5.47 100.45
4 Bandar Maharani 133532 2.05 102.56
5 Bandar Penggaram 163452 1.85 102.93
6 Bintulu 163380 3.17 113.03
7 Bukit Mertajam 221882 5.37 100.46
8 Butterworth 108893 5.40 100.37
9 Gelugor 144370 5.48 100.50
10 Georgetown 176516 5.37 100.31
11 Ipoh 692247 4.60 101.07
12 Johor Bahru 838947 1.48 103.75
13 Kajang-Sungai Chua 355092 2.98 101.77
14 Klang 936664 3.04 101.45
15 Kluang 177063 2.04 103.32
16 Kota Bahru 283657 6.12 102.24
17 Kota Kinabalu 493650 5.97 116.07
18 Kuala Lumpur 1482359 3.16 101.71
19 Kuala Terengganu 290312 5.33 103.14
20 Kuantan 382628 3.82 103.34
21 Kuching 602113 1.55 110.34
22 Kulim 184053 5.36 100.55
23 Lahad Datu 112640 5.03 118.34
24 Melaka 187111 2.21 102.25
25 Miri 241943 4.40 113.97
26 Pasir Gudang 161355 1.45 103.89
27 Petaling Jaya 535763 3.10 101.62
28 Rawang 135410 3.32 101.58
29 Sandakan 419498 5.85 118.11
30 Sekudai 178068 1.53 103.67
31 Selayang Baru 242179 3.27 101.65
32 Seremban 390647 2.71 101.95
33 Shah Alam 521219 3.07 101.56
34 Sibu 204484 2.30 111.83
35 Subang Jaya 954266 3.15 101.53
36 Sungai Ara 147146 5.33 100.27
37 Sungai Petani 240581 5.65 100.48
38 Taiping 220589 4.86 100.72
39 Tawau 327730 4.26 117.88
A suitable heading was created followed by a color palette.
Leaflet layers were then built up to create the final map:
Leaflet map of the Malaysia data frame.
Add default OpenStreetMap map tiles.
Add Circle markers of size and color matching population numbers.
Add Legend with suitable heading.
heading<- paste("Malaysia - Major Population Centres", " (", as.character(format(Sys.Date(), '%d %B %Y')), ")")
pal <- colorBin(palette = "Dark2", domain = Malaysia$pop)
leaflet(data = Malaysia) %>%
addTiles() %>%
addCircleMarkers(~long, ~lat, popup = ~pop, label = ~name,
radius= ~pop/50000, fillColor = ~pal(pop),
color="Purple", fillOpacity = 0.5, weight= 1) %>%
addLegend(position= "topright", pal= pal, values = ~pop,
group = "circles", title= heading)