The purpose of this exercise is to build a simple map with some level of interactivity using the leaflet package. A file with latitudes and longitudes for different world cities has been obtained from the following website on March 25, 2019 https://simplemaps.com/data/world-cities
Unfortunately it has not been possible to find a url for direct download of the file via download.file(), so the operation has been done manually. Also, some extra data manipulation has been necessary to adjust for the European regional settings of the computer used.
The map shows circles of a radius proportional to the population for world cities with a populatoin greater than 3 million people. Markers are clustered.
First, manual upload of the file and data manipulation to correct problems due to the computer´s European regional settings
library(stringr)
library(leaflet)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
worldcities<-read.csv("worldcities_comma.csv",sep=";")
wc<-worldcities
wc$lat<-gsub(",",".",wc$lat)
wc$lng<-gsub(",",".",wc$lng)
wc<-transform(wc,lat=as.numeric(lat),lng=as.numeric(lng))
wc_large<-filter(wc,population>3*10^6)
Finally, build the simple map using the leaflet package
wc_large %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1, radius = (wc_large$population)/100) %>%
addMarkers(clusterOptions = markerClusterOptions())
## Assuming "lng" and "lat" are longitude and latitude, respectively
## Assuming "lng" and "lat" are longitude and latitude, respectively
It can be seen that as of the date when the data set was updated, August 2018 as explained on the website, the continent with higher number of large cities was Asia, with 56.