Subscribe to DSC Newsletter

Guest blog post by vHomeInsurance.

Spatial Visualization Using R: One of the less understood aspects of R is in spatial data visualization. The below article will outline two case studies on using R to spatially visualize data.

 

Case Study 1: Using Census Data & Chloropeth R

Our first step is figuring out how to use the Census API within R. Given below are the key data Source Details from the Census ACS Data

1.            Access the Census data using the API. You can Request the census API key here

2.            For Understanding the ACS data go here

3.            The detailed ACS table data descriptions and numbers are given here

 

We use the acs.lookup function & use the keywords to find the required data across all ACS tables.   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

Results:

variable.codetable.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

 

Visualizing Census Data on Maps Using Chloropeth:

Using the Choroplethr package make it really easy to create thematic maps in R. There is native support for Choloropeth’s from US Census data & it can be accessed by the choroplethr_acs function. The R Choroplethr package does not store data locally. Instead, it uses the  Census API from the R ACS Package to get the ACS data. Here is an example to use the Census API key to get the data using choroplethr.

>library(acs)

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

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

Table B01002 has 3 columns.  We have shown the median age of each state with different gradients for each each age bracket in the below Cholropeth map.

Median Age by State

Case Study 2: Using Home Insurance Data with GGMap 

The second case study is using Home Insurance Rates data by vHomeInsurance, and using GGMap, to show average home insurance prices for some of the most populated cities in the US. The map will show more expensive home insurance cities in bigger circles and less expensive cities in smaller circles. For example, Tampa homeowner insurance is $1100 and in a bigger circle than Denver which is cheaper at $860.

 

#Reading the Home Insurance Data from File

>mapdata<-read.csv("average_home_insurance.csv",header=T)

>install.packages("ggmap")

>install.packages("mapproj")

>library(ggmap)

>library(mapproj)

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

>ggmap(map)

> TC <-ggmap(map)+geom_point(data=mapdata,alpha = .7, aes(x=longitude, y=latitude,size =Home.Insurance),color='red')+ggtitle("Average Home Insurance By City($)")

> TC

DSC Resources

Additional Reading

Follow us on Twitter: @DataScienceCtrl | @AnalyticBridge

Views: 5457

Comment

You need to be a member of Data Science Central to add comments!

Join Data Science Central

Follow Us

Videos

  • Add Videos
  • View All

Resources

© 2017   Data Science Central   Powered by

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