In this analysis we will use the basic cities data set from Simple Maps. The basic data set is free to use. The data set contains names and geographical coordinates of the biggest and most important cities in the world.
In this analysis we will use the following external libraries.
library(data.table)
library(leaflet)
library(dplyr)
Let’s download the mentioned data and load into R.
download.file(
"https://simplemaps.com/static/data/world-cities/basic/simplemaps_worldcities_basicv1.74.zip",
destfile = "cities.zip"
)
unzip("cities.zip")
cities <- fread("worldcities.csv")
We pick the 100 largest cities in the world and create labels.
cities <- cities %>%
arrange(desc(population)) %>%
mutate(population = round(population/1e6, 2)) %>%
mutate(label = paste0(city, " (", population, "M", ")")) %>%
head(100)
Finally, we visualize them on a map. The greater the circle, the greater the population of a given city.
pal <- colorNumeric(
palette = c("#fb6a4a", "#ef3b2c", "#cb181d", "#a50f15", "#800026"),
domain = cities$population)
cities %>%
leaflet() %>%
addTiles() %>%
addCircles(
lat = ~lat, lng = ~lng, label = ~label, stroke = FALSE, fillOpacity = 0.7,
radius = ~population %>% sqrt() * 50e3, color = ~pal(population)
) %>%
addLegend("topright", pal = pal, values = ~population,
title = "Population of top 100 bigest cities",
labFormat = labelFormat(suffix = "M"),
opacity = 1
)