A Data Science Central Community

This article shows step-by-step how to use R to access US Census Data, visualize it, and plot it on the map.

By Krishna Prasad, June 2014.

The article mainly focuses on how to use R to access and visualize census data. There are contributed packages that greatly enhance your ability to interact with the graphs you create in R. I will mainly focus on obtaining data from the US Census via an API connection and plotting data on different types of US maps.

**Our first step is figuring out how to use the Census API within R.**

Then we use the acs.lookup function to find the required data in all tables using key words.

For example, the following are the search results for the keywords owner, occupied, and median.

An object of class "acs.lookup"

**results:**

variable.code table.number table.name

1 B25021_002 B25021 MEDIAN NUMBER OF ROOMS BY TENURE

2 B25037_002 B25037 MEDIAN YEAR STRUCTURE BUILT BY TENURE

3 B25039_002 B25039 MEDIAN YEAR HOUSEHOLDER MOVED INTO UNIT BY TENURE

4 B25119_002 B25119 Median Household Income by Tenure

variable.name

1 Median number of rooms -- Owner occupied

2 Median year structure built -- Owner occupied

3 Median year householder moved into unit -- Owner occupied

4 Median household income in the past 12 months (in 2012 inflation-adjusted dollars) -- Owner occupied (dollars)

**Visualizing Census Data on Maps**

Using choroplethr simplifies the creation of choropleths (thematic maps) in R. It provides native support for creating choropleths from US Census data. This functionality is available with the choroplethr_acs function.

**How it Works—**

The choroplethr package does not store any data locally. Instead, it uses the R acs package to get ACS data via the Census API. This means a few things for users of choroplethr.

Table B01002 has 3 columns. Please choose the column to render:

1: Median Age by Sex: Median age -- Total:

2: Median Age by Sex: Median age -- Male

3: Median Age by Sex: Median age -- Female

Selection: 1

Fig. 1 US Census - Median Age of Home Buyers

According to the National Association of Home Builders (NAHB) study,the average buyer is expected to stay in a home for 13 years. To know the major cost paid by home buyers, we combined median home price data andaverage home insurance over a period of 13 years, and plotted the data on the US map to give a clear view of the total costs by state.

#Downloading median home price data

Downloaded Average Latitude and Longitude for US States from MAX MIND

The article mainly focuses on how to use R to access and visualize census data. There are contributed packages that greatly enhance your ability to interact with the graphs you create in R. I will mainly focus on obtaining data from the US Census via an API connection and plotting data on different types of US maps.

Then we use the acs.lookup function to find the required data in all tables using key words.

For example, the following are the search results for the keywords owner, occupied, and median.

`>acs.lookup(endyear=2012, span=5,dataset="acs", keyword= c("owner", "occupied", "median"), case.sensitive=F) `

An object of class "acs.lookup"

`endyear= 2012 ; span= 5`

variable.code table.number table.name

1 B25021_002 B25021 MEDIAN NUMBER OF ROOMS BY TENURE

2 B25037_002 B25037 MEDIAN YEAR STRUCTURE BUILT BY TENURE

3 B25039_002 B25039 MEDIAN YEAR HOUSEHOLDER MOVED INTO UNIT BY TENURE

4 B25119_002 B25119 Median Household Income by Tenure

variable.name

1 Median number of rooms -- Owner occupied

2 Median year structure built -- Owner occupied

3 Median year householder moved into unit -- Owner occupied

4 Median household income in the past 12 months (in 2012 inflation-adjusted dollars) -- Owner occupied (dollars)

Using choroplethr simplifies the creation of choropleths (thematic maps) in R. It provides native support for creating choropleths from US Census data. This functionality is available with the choroplethr_acs function.

The choroplethr package does not store any data locally. Instead, it uses the R acs package to get ACS data via the Census API. This means a few things for users of choroplethr.

`>library(acs)`

>api.key.install(key=" your secret key here")

>choroplethr_acs("B01002", "state", endyear=2012, span=5)

Table B01002 has 3 columns. Please choose the column to render:

1: Median Age by Sex: Median age -- Total:

2: Median Age by Sex: Median age -- Male

3: Median Age by Sex: Median age -- Female

Selection: 1

Fig. 1 US Census - Median Age of Home Buyers

According to the National Association of Home Builders (NAHB) study,the average buyer is expected to stay in a home for 13 years. To know the major cost paid by home buyers, we combined median home price data andaverage home insurance over a period of 13 years, and plotted the data on the US map to give a clear view of the total costs by state.

#Downloading median home price data

`>my.states=geo.make(state="*")`

>home_median _price<-acs.fetch(geography=my.states, table.number="B25077")

>write.csv(home_median _price, file=".home_median _price.csv")

Downloaded Average Latitude and Longitude for US States from MAX MIND

`#mergingthree data frames average insurance and median home price`

>Total_Cost<- merge (home_median _price,home_average_insurance,Lat_Long, by="State")

# adding median home price and 13 years average insurance

>Total_Cost$Sum<- Total_Cost $Median_Price+Total_Cost$Average_Insurance

# plottingdata on the US map

>install.packages("ggmap")

>install.packages("mapproj")

>library(ggmap)

>library(mapproj)<br< >map<- get_map(location = 'US', zoom = 4)

>ggmap(map)

> TC <- ggmap(map) + geom_point(aes(x = Longitude, y = Latitude, size = Total.Cost.in.USD), data = state_median_income, alpha = .5)+ ggtitle("Total Cost of Homes in the US")

> TC

© 2019 AnalyticBridge.com is a subsidiary and dedicated channel of Data Science Central LLC Powered by

Badges | Report an Issue | Privacy Policy | Terms of Service

**Most Popular Content on DSC**

To not miss this type of content in the future, subscribe to our newsletter.

**Technical**

- Free Books and Resources for DSC Members
- Learn Machine Learning Coding Basics in a weekend
- New Machine Learning Cheat Sheet | Old one
- Advanced Machine Learning with Basic Excel
- 12 Algorithms Every Data Scientist Should Know
- Hitchhiker's Guide to Data Science, Machine Learning, R, Python
- Visualizations: Comparing Tableau, SPSS, R, Excel, Matlab, JS, Pyth...
- How to Automatically Determine the Number of Clusters in your Data
- New Perspectives on Statistical Distributions and Deep Learning
- Fascinating New Results in the Theory of Randomness
- Long-range Correlations in Time Series: Modeling, Testing, Case Study
- Fast Combinatorial Feature Selection with New Definition of Predict...
- 10 types of regressions. Which one to use?
- 40 Techniques Used by Data Scientists
- 15 Deep Learning Tutorials
- R: a survival guide to data science with R

**Non Technical**

- Advanced Analytic Platforms - Incumbents Fall - Challengers Rise
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- How to Become a Data Scientist - On your own
- 16 analytic disciplines compared to data science
- Six categories of Data Scientists
- 21 data science systems used by Amazon to operate its business
- 24 Uses of Statistical Modeling
- 33 unusual problems that can be solved with data science
- 22 Differences Between Junior and Senior Data Scientists
- Why You Should be a Data Science Generalist - and How to Become One
- Becoming a Billionaire Data Scientist vs Struggling to Get a $100k Job
- Why do people with no experience want to become data scientists?

**Articles from top bloggers**

- Kirk Borne | Stephanie Glen | Vincent Granville
- Ajit Jaokar | Ronald van Loon | Bernard Marr
- Steve Miller | Bill Schmarzo | Bill Vorhies

**Other popular resources**

- Comprehensive Repository of Data Science and ML Resources
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- 100 Data Science Interview Questions and Answers
- Cheat Sheets | Curated Articles | Search | Jobs | Courses
- Post a Blog | Forum Questions | Books | Salaries | News

**Archives**: 2008-2014 | 2015-2016 | 2017-2019 | Book 1 | Book 2 | More

**Most popular articles**

- Free Book and Resources for DSC Members
- New Perspectives on Statistical Distributions and Deep Learning
- Time series, Growth Modeling and Data Science Wizardy
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- Comprehensive Repository of Data Science and ML Resources
- Advanced Machine Learning with Basic Excel
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- Selected Business Analytics, Data Science and ML articles
- How to Automatically Determine the Number of Clusters in your Data
- Fascinating New Results in the Theory of Randomness
- Hire a Data Scientist | Search DSC | Find a Job
- Post a Blog | Forum Questions

## You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge