I have California population density data from from the U.S. Census in an Excel file.
The spatial information is provided in ZIP Code Tabulation Areas (ZCTAs). How do I plot a population density map with that data using R?
This is what I have been doing:
library(maptools)
library(RColorBrewer)
library(classInt)
library(maps)
## set the working directory.
setwd("../Downloads")
## load the file
pop <- read.table("mre.txt", header=TRUE, row.names=1, sep=",")
#select color palette and the number colors (levels of income) to represent on the map
colors <- brewer.pal(9, "YlOrRd")
#set breaks for the 9 colors
brks<<-classIntervals(pop$POP10, n=9, style="quantile")
brks<- brks$brks
#plot the map
plot(pop, col=colors[findInterval(pop$POP10, brks,all.inside=TRUE)], axes=F)
I also tried this other way:
CaliforniaZTACs<-readShapePoly("[tl_2010_06_zcta510.shp][2]")
plot(CaliforniaZTACs)
CaliforniaZTACs
but could not associate a population density value from my dataset to the location ID of the tl_2010-06-zcta510.shp file.
SpatialPointsDataframe
in R and useplot