Reputation: 137
I hope this isn't a basic question, I've had a hard time finding online resources for using R with shapefiles. I have a shapefile of the 5 digit zip codes in Texas, specifically the one at the bottom of this page.
I'm loading the zip code data and plotting it as such:
> library(maptools)
> zipData <- readShapePoly('~/Documents/Shapefiles/zipCodesTX/tl_2009_48_zcta5.shp')
> plot(zipData)
However, this yields the full map of Texas. I'd like to pare it down to just Dallas.
I thought about using zipData@bbox
to find the max values and using xlim
and ylim
to shrink it down from there, however, this causes the y and x axis to have different amounts.
> zipData@bbox
min max
x -106.64565 -93.50844
y 25.83723 36.99566
> plot(zipData, xlim <- c(-100, -95))
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Does anyone have an idea of an easy way to do this?
Further basic shapeplot question: How does plot()
actually plot my shapefile? names(zipData)
reveals the names of the data frame columns as:
> names(zipData)
[1] "ZCTA5CE" "CLASSFP" "MTFCC" "FUNCSTAT"
[5] "ALAND" "AWATER" "INTPTLAT" "INTPTLON"
Obviously, INTPTLAT
and INTPTLON
are lat and long coordinates, but plotting these as:
> plot(zipData$INTPTLAT, zipData$INTPTLON)
yields a big black box. How exactly are maps generated using plot()
with shapefiles?
I apologize if these questions are very base, I just could not find a good resource or explanation of this.
Upvotes: 1
Views: 5424
Reputation: 68839
You can change the limits of a plot using the xlim
and ylim
arguments of the plot
function:
library("rgdal")
shp <- readOGR("tl_2009_48_zcta5.shp", "tl_2009_48_zcta5")
plot(shp, xlim=c(-97.13, -96.47), ylim=c(32.47, 33.08), col="orange")
or you can subset shp
(an object of class SpatialPolygonsDataFrame
):
zip_dallas <- c(75019, 75039, 75043, 75048, 75050, 75051, 75060, 75062, 75081,
75089, 75098, 75104, 75125, 75134, 75141, 75146, 75149, 75154,
75159, 75172, 75181, 75182, 75217, 75232, 75241, 75247, 75253,
75001, 75006, 75248, 75254, 75180, 75007, 75234, 75287, 75115,
75137, 75249, 75211, 75063, 75067, 75041, 75052, 75061, 75080,
75088, 75116, 75150, 75201, 75202, 75203, 75204, 75205, 75206,
75207, 75208, 75209, 75210, 75212, 75214, 75215, 75216, 75218,
75219, 75220, 75223, 75224, 75225, 75226, 75227, 75228, 75229,
75230, 75231, 75233, 75235, 75236, 75237, 75238, 75240, 75243,
75244, 75246, 75251, 75252, 75270, 75040, 75042, 75044, 75038,
75082, 76051)
ind <- x[["ZCTA5CE"]] %in% zip_dallas
plot(x[ind, ], col="orange")
Applied Spatial Data Analysis with R is a good reference for basic R usage and advanced spatial statistics.
Upvotes: 4
Reputation: 94182
Too many questions in there really.
First, read the R Spatial Task View for info on spatial data in R.
Then maybe read an introduction to spatial data in R by me: http://www.maths.lancs.ac.uk/~rowlings/Teaching/UseR2012/introductionTalk.html
Then notice that you used <-
when you should have used =
:
plot(zipData, xlim <- c(-100, -95))
Upvotes: 0