3

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:

dataset

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.

4
  • 1
    What kind of spatial information is in your excel sheet? Point locations? Just convert it to SpatialPointsDataframe in R and use plot
    – Curlew
    Aug 14, 2013 at 10:23
  • Quick google let me stumble on this website where you apparently can download shapefile: census.gov/geo/reference/zctas.html See if this helps you. Based on the ZCTA you could make a join then
    – Curlew
    Aug 14, 2013 at 10:32
  • 1
    Also please show us what you have tried so far in terms of your R code. Aug 14, 2013 at 10:59
  • 1
    @AndreSilva: I added the dataset. Thanks for your help.
    – Cost
    Aug 22, 2013 at 14:23

1 Answer 1

2

The more populated area seems to be the regions of San Francisco and Los Angeles, right?

library(ggplot2)

pop <- read.table("mre.txt", header=TRUE, row.names=1, sep=",")
pop <- data.frame(pop)

ggplot(data=pop,aes(x=LONG,y=LAT)) + geom_point(data=pop, aes(size=POP10)) + theme_bw()

enter image description here

You can also add a Google Maps (or OpenStreetMap) image as background with R get_map function and plot it with ggmap package + your data.

library(ggmap)

x <- get_map(location="California", zoom=6, maptype="terrain")

ggmap(x, extent="normal") + geom_point(data=pop, aes(x=LONG,y=LAT,size=POP10)) + theme_bw()

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.