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
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
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.
>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
>map<- get_map(location = 'US', zoom = 4)
> 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($)")