#Get required mapping packages install.packages(c("choroplethr", "choroplethrMaps")) library(choroplethr) library(choroplethrMaps) #Pull data from Census Bureau API data_web<-readLines("http://api.census.gov/data/2014/acs5/profile?get=NAME,DP03_0062E,DP03_0065E,DP03_0067E,DP03_0069E,DP03_0071E,DP03_0073E&for=county:*",warn="F") #Clean up the data, get rid of \\ temp1<-gsub("[^[:alnum:], _]", '', data_web) #Split string by ",", i.e. expandes single element into many temp2<-strsplit(temp1,",") #Need to realign header row as "," was used to seperate County Name and State, but header only had Name element temp2[[1]]<-c(temp2[[1]][1],"State",temp2[[1]][2:9]) #Using do.call() to iterate through list elements a data.frame, note skip 1st one as it contains header information data <- as.data.frame(do.call(rbind, temp2[-1]), stringsAsFactors=FALSE) #Grab header information and assign variable names to data.frame names(data) <- temp2[[1]] #Get rid of Puerto Rico data <- data[data$state != "72",] #################################### #Plotting... #The county_choropleth() needs # 1. region column: state fips + county fips; type = numeric # 2. value column: the data being plotted; type = numeric #################################### #Create modified data.frame with only two columns; set value to column being plotted plot.data<-data.frame(region=paste(data[,9],data[,10],sep=""),value=data[,3], stringsAsFactors=FALSE) #Forcing numeric data type plot.data<-data.frame(region=as.numeric(plot.data[,1]),value=as.numeric(plot.data[,2])) #Creat plot using county_choropleth() function county_choropleth(plot.data,title="Median Household Income ")