Hi Tom and everyone,
First, Tom an excellent little google website you pointed out!
Then below is what I have used to merge and have fun with data frames.
My apologies in advance, Netscape Communicator will likely forget
some line feeds.
##############################
# . read in your basic state zip file with whatever columns
##############################
state_alldata_df <- read.csv ("state_info.csv", header = TRUE, sep = ",")
##############################
# . get one small state to experiment with WY has about 550,000 people
and 23 counties
# people and likely fewer zip codes.
# . assuming your state file has state abbreviation as
# ST_ABBREV, ST_NAME, and ZIP, CUST_CNT plus other non needed cols.
# . in the example below, we drop ST_ABBREV in the second data frame
##############################
wy_df <- state_alldata_df[state_alldata_df$ST_ABBREV=="WY",
c("STATE_NAME", "ZIP", "CUST_CNT")]
remove(state_alldata_df) # clean up data frames we don't need
names(wy_df) # list data frame columns
##############################
# . read in the shapefile see below for more general info, especially
the 1st line readShapePoly code
# . you can also get more read shapefile examples from r-spatial.source
forge below
# .
http://www.nceas.ucsb.edu/scicomp/GISSeminar/UseCases/PolygonDissolve/PolygonDissolve.html
##############################
library(sp)
library(maptools) # for geospatial services; automatically loads
foreign and sp
library(rgdal) # for map projection support; automatically
loads sp
library(PBSmapping) # for GIS_like geospatial object manipulation
/ analysis including poly
zip1_sp <- readShapePoly("allstates.shp", IDvar="ZIP",
proj4string=CRS("+proj=aea +ellps=GRS80 +datum=WGS84"))
names(zip1_sp) # list spatial data frame columns, it has lots
of other info besides cols
###########################################################
# . good explanation
# http://www.ats.ucla.edu/stat/R/modules/subsetting.htm
# . select all cols from wy_df by default, so your $CUST_CNT is here
###########################################################
zip2_sp <- zip1_sp [zip1_sp$ZIP %in% wy_df$ZIP,]
names(zip2_sp) # list spatial data frame columns
sapply(slot(zip2_sp, "polygons"), function(x) slot(x, "ID")) # what are
the sp "ID"s?
###########################################################
# . plotting time. Example code from
# http://r-spatial.sourceforge.net/gallery/#fig13.R
###########################################################
plot(zip2_sp)
invisible(text(getSpPPolygonsLabptSlots(zip2_sp),
labels=as.character(zip2_sp$CUST_CNT), cex=0.4))
title("Wyoming Customer count", sub = "Something at the bottom of the
graph",
cex.main = 2, font.main= 4, col.main= "blue",
cex.sub = 0.75, font.sub = 3, col.sub = "red")
Hope this helps, now back to myself figuring out "spplot.polygons"
Jim Burke
Hengl, T. wrote:
Dear Alina,
If you load two datasets to R and you want to merge them via some key (e.g. zip code), you have to consider using the "merge" method e.g.:
spdata <- merge(x=data.frame(ZIP=zipshape$ZIP, X=zipshape$X, Y=zipshape$Y),
y=data.frame(ZIP=customers$ZIP, V=customers$V), by=ZIP, all.y=F)
coordinates(merge) <-~X+Y
You could also consider using the Google service to geocode the addresses (I do not know how accurate is your US zip shape file, Google data is pretty accurate).
You should be able to figure out from this example what you have to do automate retrieval of coordinates from addresses:
http://spatial-analyst.net/wiki/index.php?title=Mapping_research_hot-spots#Geocoding_addresses
HTH
Tom Hengl
________________________________
From: r-sig-geo-boun...@stat.math.ethz.ch on behalf of Alina Sheyman
Sent: Tue 3/3/2009 8:49 PM
To: R-sig-Geo@stat.math.ethz.ch
Subject: [R-sig-Geo] mapping by zip codes
I'm very new to creating maps using shapefiles, so this question might be
pretty basic.
I'm trying to create a map of the US, divided up by zip codes, where i then
map number of customers by zip code.
I located shapefiles for US with zip codes (although at this point only for
individual states, and not the entire country) and read it into R using
maptools,
but now I need to apply my data (which I read into R as a separate data
frame and not a shapefile) to that map. Which is the part where I get
completely lost and not sure how to proceed. If anyone could give me any
pointers I'd really appreciate it.
thank you
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo