How to Search for Census Data from R

In my course Learn to Map Census Data in R I provide people with a handful of interesting demographics to analyze. This is convenient for teaching, but people often want to search for other demographic statistics. To address that, today I will work through an example of starting with a simple demographic question and using R to answer it.

Here is my question: I used to live in Japan, and to this day I still enjoy practicing Japanese with native speakers. If I wanted to move from San Francisco to a part of the country that has more Japanese people, where should I move?

Step 1: Find the Table for the Data

Data in the census bureau is stored in tables. One way to find the table for a particular metric is to use the function ?acs.lookup from the acs package. (Note that to run this code you will need to get and install a census API key; I explain how to do that here).

> library(acs)
> acs.lookup(keyword = "Japanese", endyear = 2013)
An object of class "acs.lookup"
endyear= 2013  ; span= 5 
  variable.code table.number                                                                                   
1    B02006_009       B02006                                                Asian Alone By Selected Groups                                       Japanese
2    B16001_069       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs                                      Japanese:
3    B16001_070       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs            Japanese: Speak English 'very well'
4    B16001_071       B16001 Language Spoken at Home by Ability to Speak English for the Population 5+ Yrs  Japanese: Speak English less than 'very well'

The Census Bureau has two “Japanese” tables: the first relates to race and the second to language. For simplicity, let’s focus on race (B02006). The “_009” at the end indicates the column of the table; each column tabulates a different Asian nationality. 

Step 2: Get the Data

There are a few ways to get the data from that table into R. One way is to use the function ?acs.fetch in the acs package. If your end result is to map the data with the choroplethr package, however, you might find it easier to use the function ?get_acs_data in the choroplethr package:

> library(choroplethr)
> l = get_acs_data("B02006", "county", column_idx=9)

What’s returned is a list with 2 elements. The first element is a data frame with the (region, value) pairs. The second element is the title of the column:

List of 2
 $ df :'data.frame': 3143 obs. of 2 variables:
 ..$ region: num [1:3143] 1001 1003 1005 1007 1009 ...
 ..$ value : num [1:3143] 10 25 0 0 0 0 0 103 2 19 ...
 $ title: chr "Asian Alone By Selected Groups: Japanese"

Step 3: Analyze the Data

The first way to analyze the data is to simply look at the data frame:

> df = l[[1]]
> head(df)
region value
1 1001 10
2 1003 25
3 1005 0
4 1007 0
5 1009 0
6 1011 0

People who have taken my course will recognize the regions as FIPS County Codes. We can use a boxplot to look at the distribution of values:



I draw two conclusions from this chart: 1) the median is very low and 2) there are two very large outliers.

To find out the names of the outliers we need to convert the FIPS Codes to English. We can do that by merging df with the data frame ?county.regions.

> data(county.regions)
> head(county.regions)
   region county.fips.character state.fips.character
1    1001                 01001     autauga    alabama                   01        AL
36   1003                 01003     baldwin    alabama                   01        AL
55   1005                 01005     barbour    alabama                   01        AL
15   1007                 01007        bibb    alabama                   01        AL
2    1009                 01009      blount    alabama                   01        AL
16   1011                 01011     bullock    alabama                   01        AL
> df2 = merge(df, county.regions)
> df2 = df2[order(-df2$value), ]
> head(df2)
     region  value county.fips.character state.fips.character
548   15003 150984                 15003    honolulu     hawaii                   15        HI
205    6037 103180                 06037 los angeles california                   06        CA
216    6059  33211                 06059      orange california                   06        CA
229    6085  28144                 06085 santa clara california                   06        CA
2971  53033  21493                 53033        king washington                   53        WA
223    6073  18592                 06073   san diego california                   06        CA

So the outliers are Honolulu county and Los Angeles county. San Francisco isn’t even in the top 6. So if I ever decide to give up my career in technology for a career focused on Japanese, I should move to Honolulu!

It’s also easy to create a choropleth map of the values. This allows us to see the geographic distribution of the values.

county_choropleth(df, title = "2012 County Estimates:\nNumber of Japanese per County")


According to this map, by living on the west coast I am already in a part of the country with a high concentration of Japanese people.


If you wind up using this blog post to do an analysis of your own, or have difficulty adapting this code to your own purposes, please leave a comment below. I’m always interested in hearing what my readers are working on.

A final note to my Japanese friends: どう思いますか?アメリカで一番興味がある場所はホノルルとロサンゼルスですか?口コミしてください!

Eric Yount says December 18, 2015

Hello! I was following along with your code and everything went well until I hit the final step. When I go to create the county_choropleth, I get the following error message: “Error: Don’t know how to add self$ggplot_polygon to a plot”. I’ve searched with no luck on how to fix it. Any ideas? Thanks!

    Ari Lamstein says December 18, 2015

    I just reran the code and do not get that error. Try updating your version of R and packages (type “update.packages”) and trying again. If that doesn’t work, please post your entire code on the choroplethr google group:!forum/choroplethr

    Ari Lamstein says December 20, 2015

    Actually, it looks like this is related to the new release of ggplot2. I think that I fixed this in the development version of choroplethr. Can you please install it and try again (

Comments are closed