Map prisons in the United States

February 4, 2018

(This article was first published on R & Census, and kindly contributed to R-bloggers)

Despite 1 in every 110 adults are locked behind bars, most ordenary people have no idea where are these prisons. This post helps you find out the location of and number of inmates in federal prisons, state prisons, and local jails.

The data are extracted from Census 2010. According to the census, a total number of 2,263,602 persons were incarcerated in the United States. Among them, 172,020 were in federal prisons, 1,248,167 in state prisons, and 682,043 in local jails. In addition to the total number, Census 2010 also provides the number of inmates in small areas down to census tract and county subdivision. With the small area data, we can map them on interactive maps.

We use totalcensus package to extract prison data with spatial resolution at county subdivision level and use leaflet package to make interactive map. Drag, zoom in and click on each data point to find more information about a prison.


# load packages
# follow instruction on to setup 
# totalcensus package

# federal and state prison location and population
# search tables for tables of prison population
# search_tablecontents("decennial")
    # PCT0200005 for federal prison
    # PCT0200006 for state prison
    # PCT0200007 for local jail

# read prison population data and format data
pris_pop <- read_decennial(
    year = 2010,
    states = "US",
    table_contents = c(
        "total = PCT0200003", 
        "fed_pris = PCT0200005", 
        "state_pris = PCT0200006",
        "local_jail = PCT0200007"
    geo_headers = "NAME",
    summary_level = "county subdivision",
    show_progress = FALSE
) %>%
    # remove county subdivisions that has no prison popualation
    filter(fed_pris != 0 | state_pris != 0 | local_jail != 0) %>%
    mutate(fed_pris = ifelse(fed_pris == 0, NA, fed_pris)) %>%
    mutate(state_pris = ifelse(state_pris == 0, NA, state_pris)) %>%
    mutate(local_jail = ifelse(local_jail == 0, NA, local_jail)) %>%
    select(lon, lat, NAME, fed_pris, state_pris, local_jail) %>%
    gather(key = "type", value = "inmates", -c(lon, lat, NAME)) %>%
    filter(! %>%
    mutate(type = factor(
        type, levels = c("local_jail", "state_pris", "fed_pris")
    )) %>%

# format color for leaflet map
pal <- colorFactor(
    palette = c("green", "blue", "red"),
    domain = pris_pop$type

# make interactive map
leaflet(pris_pop) %>%
    fitBounds(-122, 25, -70, 50) %>%
    addTiles() %>%
    addCircles(lng = ~lon, lat = ~lat, radius = ~inmates,
               color = ~pal(type), popup = ~paste(inmates, "inmates,", NAME)) %>%
    addLegend(position = "bottomleft", pal = pal, values = ~type,
              title = "")

To leave a comment for the author, please follow the link and comment on their blog: R & Census. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)