Create a web page using R Markdown that features a map created with Leaflet.
Host your webpage on either GitHub Pages, RPubs, or NeoCities.
Your webpage must contain the date that you created the document, and it must contain a map created with Leaflet. We would love to see you show off your creativity!
library(leaflet)
# Major cities in China
# Data source: https://simplemaps.com/data/cn-cities
cn_cities <- read.csv("D:/R/data/cn_cities.csv",
header=TRUE,
#colClasses="character",
strip.white=FALSE,
stringsAsFactors=FALSE,
na.strings=c("NA", "NaN", "N/A", "", "#VALUE!", "#DIV/0!"),
encoding='UTF-8')
dim(cn_cities)
## [1] 2187 9
str(cn_cities)
## 'data.frame': 2187 obs. of 9 variables:
## $ city : chr "Shanghai" "Beijing" "Guangzhou" "Shenzhen" ...
## $ lat : num 31.2 39.9 23.1 22.5 30.6 ...
## $ lng : num 121 116 113 114 114 ...
## $ country : chr "China" "China" "China" "China" ...
## $ iso2 : chr "CN" "CN" "CN" "CN" ...
## $ admin : chr "Shanghai" "Beijing" "Guangdong" "Guangdong" ...
## $ capital : chr "admin" "primary" "admin" "minor" ...
## $ population : int 14987000 11106000 8829000 7581000 7243000 7180000 6461000 4787000 4528000 4123000 ...
## $ population_proper: int 14608512 7480601 3152825 1002592 4184206 3766207 3967028 3512192 4528000 3950437 ...
# Major cities in China in cluster view
leaflet(cn_cities) %>% addTiles() %>% addMarkers(
clusterOptions = markerClusterOptions())
# The top 50 largest cities in China by population
cities <- cn_cities[order(-cn_cities$population),]; head(cities)
## city lat lng country iso2 admin capital population
## 1 Shanghai 31.22222 121.4581 China CN Shanghai admin 14987000
## 2 Beijing 39.92882 116.3889 China CN Beijing primary 11106000
## 3 Guangzhou 23.11667 113.2500 China CN Guangdong admin 8829000
## 4 Shenzhen 22.53333 114.1333 China CN Guangdong minor 7581000
## 5 Wuhan 30.58333 114.2667 China CN Hubei admin 7243000
## 6 Tianjin 39.14222 117.1767 China CN Tianjin admin 7180000
## population_proper
## 1 14608512
## 2 7480601
## 3 3152825
## 4 1002592
## 5 4184206
## 6 3766207
cities <- cities[c(1:50),]
cities <- data.frame(name=cities$city, pop=cities$population,
lat=cities$lat, lng=cities$lng)
cities %>% leaflet() %>% addTiles() %>% addCircles(
weight=1, color="#49b2b2", radius=sqrt(cities$pop) * 50)