Cities boomed and doomed

[This article was first published on R & Census, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



Population in the United States grew by 3.76% from 313.91 million in 2012 to 325.72 million in 2017. The growth, however, is not uniform geographically. One of the geographical areas of particular interest is the city. Some cities are shrinking in population while some others exploding. The purpose of this post is to find cities that decrease and increase most in population. In addition, we will show the population and its change of all cities in an interactive map.

We will use population data of cities published in the American Community Survey 5-year estimates. The numbers are 5-year rolling average, for example, the data published in 2017 are averaged over the 5 year period of 2013 – 2017. ACS 1-year estimates provide data of one particular year but unfortunately they only cover cities with population greater than 65000.

Read data from summary files

In ACS data, city is presented by summary level “place”. The summary level “place” also includes census designated places (CDPs), which strictly speaking are not cities. So we will select only those places that have “city” in their names. We will also keep cities with population greater than 5000 in the 2008-2012 or 2013-2017 ACS 5-year estimates. Let’s get the population data of cities using totalcensus package. The codes below read 2012 and 2017 ACS 5-year population data of all places, join the data, and calculate the percentage change in population from 2012 to 2017.

library(totalcensus)
library(data.table)
library(magrittr)
library(leaflet)

# read population data of 2007-2012 and 2013-2017 ACS 5-year 
p2012 < - read_acs5year(year = 2012, 
                     states = states_DC,
                     summary_level = "place", 
                     show_progress = FALSE) %>%
    .[, .(GEOID, p1 = population)]

p2017 < - read_acs5year(year = 2017, 
                     states = states_DC,
                     summary_level = "place", 
                     show_progress = FALSE) %>%
    .[, .(GEOID, NAME, lon, lat, p2 = population)]

# joint data, calculate change of pupulation for cities with population over 5000
popul < - p2012[p2017, on = .(GEOID)]  %>%
    # keep only those population greater than 1000, too many microcities
    .[p1 > 5000 | p2 > 5000] %>%
    # keep 48 states for better plotting
    .[lon > -130 & lon < -68] %>%
    # keep places that have "city" in their names
    .[NAME %like% "city"] %>%
    # calculate percentage change 
    .[, change := round(100 * (p2 - p1) / p1, 2)] %>%
    .[order(change)]

Top 10 doomed and boomed cities

Illinois has 3 cities in the top 10 list of doomed cities and Texas has 4 in the top 10 list of boomed cities. Surprisingly, Texas also has 2 cities in the list of top 10 doomed cities. The tables below give city names, population in 2012 and 2017, and the population change.

The doomed: top 10 cities with the most population loss

kable(popul[1:10, .(NAME, p2012 = p1,  p2017 = p2, "change(%)" = change)])
NAMEp2012p2017change(%)
Sumner city, Illinois50733285-35.25
San Elizario city, Texas142079213-35.15
Gatesville city, Texas1577912426-21.25
Rio Communities city, New Mexico56254593-18.35
Sullivan city, Missouri78306587-15.87
Genoa city, Illinois56934808-15.55
Avenal city, California1534113119-14.48
Susanville city, California1738415286-12.07
Shelbyville city, Illinois52674650-11.71
Trinidad city, Colorado90328054-10.83

The boomed: top 10 cities with the most population gain

kable(popul[(.N-9):.N, .(NAME, p2012 = p1,  p2017 = p2, "change(%)" = change)])
NAMEp2012p2017change(%)
Selma city, Texas5637921763.51
Wilmore city, Kentucky3736622666.65
Clarkston city, Georgia76121270266.87
Buda city, Texas78351325369.15
Cibolo city, Texas155412660271.17
Kirkland city, Washington490908677276.76
Sanford city, Maine1008420920107.46
Chamblee city, Georgia1329728433113.83
Watford City city, North Dakota20885441160.58
Fulshear city, Texas13656203354.43

Interactive mapping

We divide the population change into 5 groups: obvious decline (< -2%), marginally change (-2% – 2%), average gain (2% – 6%), significant increase (6% – 10%), and extraordinary gain (> 10%).

popul < - popul[!is.na(change), group := "< -2%"] %>%
    .[change > -2, group := "-2% - 2%"] %>%
    .[change > 2, group := "2% - 6%"] %>%
    .[change > 6, group := "6% - 10%"] %>%
    .[change > 10, group := "> 10%"] %>%
    .[, group := factor(group, levels = c(
        "< -2%", "-2% - 2%", "2% - 6%", "6% - 10%", "> 10%"
    ))] %>%
    .[order(-p2)]

And here is the code to make interactive mapping using leaflet. Zoom in and click to view the details of each city.

pal < - colorFactor(
    palette = c("blue", "green", "cyan", "orange", "magenta"),
    domain = popul$group
)
leaflet(popul) %>% addTiles() %>%
    addCircles(lng = ~lon, lat = ~lat, weight = 1,
               radius = ~p2^0.5 * 30, 
               popup = paste0(popul$NAME, "
", "2017 population: ", popul$p2, "
", "5-year population change: ", popul$change, "%"), color = ~pal(group), fillOpacity = 0.5) %>% addLegend("bottomleft", pal = pal, values = ~group, title = "population change", opacity = 1)

To leave a comment for the author, please follow the link and comment on their blog: R & Census.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)