This is an interactive map showing the capitals of the 48 contiguous states and their populations. The map was constructed using Leaflet.The size of the circles are proportional to the population of the capitals.The data was obtained from the “maps” package “cities.us” dataset.
To navigate, click on a green or yellow cluster marker to expand it and reveal individual markers. Then click on an individual marker to reveal the name of the city and it’s population. Alternately, click on the circles to reveal the same information.
#load libraries
library(leaflet);library(dplyr);library(maps)
#Load dataset and prepare for mapping
data("us.cities")
CapLatLng <- filter(us.cities,capital == 2 & country.etc !="HI" & country.etc != "AK") %>%
select(name,pop,lat,long) %>%
mutate(popf = formatC(pop,big.mark = ",", big.interval = 3L)) %>%
mutate(name_pop = paste(name,"Pop:", popf,sep = " "))
#Set Circle colors based on population
radius <- sqrt(CapLatLng$pop)*120
circleColor <- cut(radius,3,labels = c("red","purple", "blue"))
#Generate and plot Map
CapLatLng %>% leaflet() %>% addTiles() %>%
addMarkers(popup = CapLatLng$name_pop,clusterOptions = markerClusterOptions()) %>%
addCircles(popup=CapLatLng$name_pop,weight=1, radius=sqrt(CapLatLng$pop)*120,
color=circleColor) %>%
addLegend(labels = c("Small", "Medium","Large"), color = c("red","purple","blue"),
title = "Population", opacity = 0.3)