This small demonstration of the capabilities of the R package leaflet, is part of the course project of the “Developing Data Projects” class.
The data was sourced from simplemaps.com which released this supset of data free of charge under the MIT license.
It contains data of the 78 most important cities of Japan. The data can be downloaded here in csv, xlsx or json format.
dataUrl <- url("https://simplemaps.com/static/data/country-cities/jp/jp.csv")
jpCitiesData <- read.csv(dataUrl)
str(jpCitiesData)
## 'data.frame': 77 obs. of 9 variables:
## $ city : Factor w/ 77 levels "Akita","Aomori",..: 65 51 76 43 8 57 58 17 32 26 ...
## $ lat : num 35.7 34.7 35.4 35.2 33.6 ...
## $ lng : num 140 136 140 137 130 ...
## $ country : Factor w/ 1 level "Japan": 1 1 1 1 1 1 1 1 1 1 ...
## $ iso2 : Factor w/ 1 level "JP": 1 1 1 1 1 1 1 1 1 1 ...
## $ admin : Factor w/ 47 levels "Aichi","Akita",..: 41 33 19 1 7 12 24 11 22 13 ...
## $ capital : Factor w/ 4 levels "","admin","minor",..: 4 2 2 2 2 2 2 2 2 2 ...
## $ population : int 35676000 11294000 3697894 3230000 2792000 2544000 2250000 2045000 1805000 1528478 ...
## $ population_proper: int 8336599 2592413 3697894 2191279 1392289 1861786 1037562 1143841 1459640 1528478 ...
Using the leaflet package we can plot the biggest cities of japan.
# expression saved for later use
popUpExp <- "paste(\"City: \", jpCitiesData$city, \"<br>\",
\"Population: \", jpCitiesData$population, \"<br>\",
\"Admin: \", jpCitiesData$admin, \"<br>\")"
library(leaflet)
myMap <- leaflet(jpCitiesData)
myMap <- addTiles(myMap)
myMap <- addMarkers(myMap, clusterOptions = markerClusterOptions(),
popup = eval(parse(text = popUpExp)))
myMap <- addCircles(myMap, weight = 1, radius = sqrt(jpCitiesData$population) * 10)
myMap
For cleaner code we also could use the piping notation R provides.
jpCitiesData %>%
leaflet() %>%
addTiles() %>%
addMarkers(clusterOptions = markerClusterOptions(), popup = eval(parse(text = popUpExp))) %>%
addCircles(weight = 1, radius = sqrt(jpCitiesData$population) * 10)