Mapping Census Data


This tutorial demonstrates how to plot census data on a U.S. map. Specifically, it uses data from the 2021 Current Population Survey and Civic Engagement and Volunteering supplement to plot levels of volunteerism by state.

Step 1
Make sure you have the following packages installed and called.

library(tidycensus)
library(tigris)
library(tidyverse)
library(viridis)
library(censusapi)

Then,get an API key from https://api.census.gov/data/key_signup.html

Enter the key into census_api_key("").

census_api_key("14a7be603c3395bef1d668c965c4862d41df050f")

To get a list of all surveys available through the API key, try this.

apis <- listCensusApis()

View(apis)

I know that I want to use data from the 2021 Current Population Survey Civic Engagement and Volunteering Supplement. So I will search for the survey by Title.

grep("volunteering", apis$title, ignore.case=TRUE)
##  [1]   84  189  470  520  588  650  766  812  868  898  927  955  981 1003 1026
## [16] 1049
print(apis[84, ])
##                                                                title
## 84 Current Population Survey: Volunteering and Civic Life Supplement
##                 name vintage      type        temporal
## 84 cps/volunteer/sep    2021 Microdata 2021-09/2021-09
##                                                  url              modified
## 84 http://api.census.gov/data/2021/cps/volunteer/sep 2022-06-03 00:00:00.0
##                                                                                                                                                                                                                                                         description
## 84 Information on a variety of topics that assess civic health: interactions with family, friends, neighbors; political engagement; group membership and participation; frequency of volunteering activity; and donations to political or charitable organizations.
##               contact
## 84 dsd.cps@census.gov


Now I know what titles to use when telling which dataset to use. I also need to find out how the variables I want to use are labeled.

cps_vars <- listCensusMetadata(name="cps/volunteer/sep", type="variables", vintage=2021)
#View(cps_vars)

When using “get_census”, fill in the following information:

name: the name of the survey as listed above
vintage: the year of the survey
vars: the variables you wish to include. It is helpful to cast the variables in a separate line of code if you are renaming them.
region: the type of geography to group by

v1<-c(st="GESTFIPS", vol.kids.act="PES16A", hours.vol="PTS16E", vol.freq="PES16D", encouraged.vol="PESWP2", vol.status="PRSUPVOL", volunteer="PES16")

CenDF <- getCensus(key="14a7be603c3395bef1d668c965c4862d41df050f", name = "cps/volunteer/sep", vintage = "2021", 
    vars = v1, 
    region = "state")

head(CenDF)
##   state GESTFIPS PES16A PTS16E PES16D PESWP2 PRSUPVOL PES16
## 1     5        5     -1     -1     -1     -1       -1    -1
## 2     5        5     -1     -1     -1     -1       -1    -1
## 3    13       13      2     -1     -1     -1        2     2
## 4    13       13      2     -1     -1     -1        2     2
## 5    13       13      1      4      5      2        1     2
## 6    13       13      2     -1     -1      2        2     2
dim(CenDF)
## [1] 103858      8

This survey shows individual reponses rather than aggregate data. Now we need to take a look at the survey to see what the numbers mean for each response.
This is where it can be found online.


We can see that for question PES16 “1” represents a “Yes”, meaning someone has volunteered in the past year. A “1” for PES16A also signifies someone did a sort of volunteer service. So, we will want to find the sum of all records that have a “1” for PES16 or PES16A grouped by state.

CenDF$Has.vol<-( CenDF$PES16=="1"| CenDF$PES16A=="1")
CenDF$Has.vol<-as.numeric(CenDF$Has.vol)
head(CenDF)
##   state GESTFIPS PES16A PTS16E PES16D PESWP2 PRSUPVOL PES16 Has.vol
## 1     5        5     -1     -1     -1     -1       -1    -1       0
## 2     5        5     -1     -1     -1     -1       -1    -1       0
## 3    13       13      2     -1     -1     -1        2     2       0
## 4    13       13      2     -1     -1     -1        2     2       0
## 5    13       13      1      4      5      2        1     2       1
## 6    13       13      2     -1     -1      2        2     2       0
CenDF1<-
CenDF%>%
  dplyr::group_by(GESTFIPS)%>%
  dplyr::summarize(n=n(),
                   vol=sum(Has.vol),
                   prop.vol=(vol/n)) %>%
  as.data.frame(.) %>% 
  dplyr::mutate_if(is.numeric, round, 2)%>%
  rename(FIPS=GESTFIPS)

head(CenDF1)
##   FIPS    n vol prop.vol
## 1    1 2223 186     0.08
## 2   10 1109 172     0.16
## 3   11 1519 312     0.21
## 4   12 4914 345     0.07
## 5   13 2214 216     0.10
## 6   15 1592 154     0.10


Next we will populate a United States Map.

library(tigris)

options(tigris_use_cache = TRUE)
options(tigris_class = "sf")
# We can also use the state number in place of the state. For instate Pennsylvanian is the 42 state in US. 
us <- states(cb=T)%>%
  rename(FIPS=GEOID)
view(us)

ggplot(us)+ # we can use the state number in pace of the state
  geom_sf()+
  theme_void()+
  theme(panel.grid.major = element_line(colour = "transparent"))+
  labs(title = "% Population Volunteering by State")


Since the states are not named in our initial dataset, we will have to combine the US map with our data set so it knows how to map the observations. Single digit FIPS in the initial data set will need to be changed to two digits in order to match when the data sets are joined.

CenDF1%>%
  arrange(FIPS)
##    FIPS    n vol prop.vol
## 1     1 2223 186     0.08
## 2    10 1109 172     0.16
## 3    11 1519 312     0.21
## 4    12 4914 345     0.07
## 5    13 2214 216     0.10
## 6    15 1592 154     0.10
## 7    16 1742 255     0.15
## 8    17 2851 388     0.14
## 9    18 1807 251     0.14
## 10   19 1273 198     0.16
## 11    2 1232 171     0.14
## 12   20 1376 230     0.17
## 13   21 1288 136     0.11
## 14   22 2343 237     0.10
## 15   23  829 143     0.17
## 16   24 1192 146     0.12
## 17   25 2177 226     0.10
## 18   26 2477 317     0.13
## 19   27 1326 238     0.18
## 20   28 1901 215     0.11
## 21   29 1575 257     0.16
## 22   30 1777 286     0.16
## 23   31 1423 195     0.14
## 24   32 1457 111     0.08
## 25   33 1307 203     0.16
## 26   34 1868 204     0.11
## 27   35 1762 202     0.11
## 28   36 3666 354     0.10
## 29   37 2609 274     0.11
## 30   38 1662 224     0.13
## 31   39 2647 344     0.13
## 32    4 1691 164     0.10
## 33   40 1555 165     0.11
## 34   41 1608 236     0.15
## 35   42 2706 334     0.12
## 36   44  880  93     0.11
## 37   45 1729 177     0.10
## 38   46 1165 201     0.17
## 39   47 1871 228     0.12
## 40   48 6176 598     0.10
## 41   49 1861 312     0.17
## 42    5 1851 192     0.10
## 43   50 1482 248     0.17
## 44   51 1881 207     0.11
## 45   53 1968 247     0.13
## 46   54 1961 214     0.11
## 47   55 1418 196     0.14
## 48   56 1665 292     0.18
## 49    6 8729 808     0.09
## 50    8 1423 161     0.11
## 51    9 1100 130     0.12
CenDF1$FIPS[1]<-paste("0", CenDF1$FIPS[1], sep = "")
CenDF1$FIPS[11]<-paste("0", CenDF1$FIPS[11], sep = "")
CenDF1$FIPS[49:51]<-paste("0", CenDF1$FIPS[49:51], sep = "")
CenDF1$FIPS[32]<-paste("0", CenDF1$FIPS[32], sep = "")
CenDF1$FIPS[42]<-paste("0", CenDF1$FIPS[42], sep = "")

final <- inner_join(us, CenDF1, by=c("FIPS"="FIPS"))%>%
arrange(desc(prop.vol))

Here we can see a list of which states have the highest levels of volunteerism.

final%>%
ggplot( aes(x = prop.vol, 
y = reorder(NAME, prop.vol))) +
geom_point(color = "black", size = 2) +
labs(title = "% Population Volunteered by State",
subtitle = "US CPS 2021",
x = "Volunteer", y="") +
theme(panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank())


Let’s take a look at some summary statistics. On average, 13% of people from each state volunteer. The highest level of volunteerism is 21% in the District of Colombia. The lowest is 7% in Florida.

summary(final$prop.vol)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0700  0.1050  0.1200  0.1282  0.1550  0.2100


Finally, we can use ggplot to view the data in the map!

library(tigris)
ggplot(final)+
  geom_sf(aes(fill = prop.vol))+
   coord_sf(xlim = c(-130, -60), ylim = c(25, 50)) +
  theme_void()+
  theme(panel.grid.major = element_line(colour = "white"))+
  labs(title="Percent Population Volunteering by State", caption="Source: US Census/CPS 2021")+
scale_fill_viridis(direction=-1)


References:
https://walker-data.com/tidycensus/articles/basic-usage.html
https://rpubs.com/WSKQ/896404
https://ggplot2-book.org/scales-colour