简体   繁体   English

在R中绘制shapefile时如何限制边界

[英]How to limit boundaries when plotting a shapefile in R

I hope this isn't a basic question, I've had a hard time finding online resources for using R with shapefiles. 我希望这不是一个基本问题,我很难找到使用R和shapefile的在线资源。 I have a shapefile of the 5 digit zip codes in Texas, specifically the one at the bottom of this page . 我在德克萨斯州有一个5位数邮政编码的shapefile,特别是本页底部的一个。

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查找最大值并使用xlimylim从那里缩小它,但是,这会导致y轴和x轴具有不同的量。

> 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? 进一步的基本shapeplot问题: plot()如何实际绘制我的shapefile? names(zipData) reveals the names of the data frame columns as: names(zipData)将数据框列的名称显示为:

> 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: 显然, INTPTLATINTPTLON是lat和long坐标,但是将它们绘制为:

> plot(zipData$INTPTLAT, zipData$INTPTLON)

yields a big black box. 产生一个大黑盒子。 How exactly are maps generated using plot() with shapefiles? 如何使用带有shapefile的plot()生成地图?

I apologize if these questions are very base, I just could not find a good resource or explanation of this. 我很抱歉,如果这些问题非常基础,我就找不到好的资源或解释。

You can change the limits of a plot using the xlim and ylim arguments of the plot function: 您可以使用plot函数的xlimylim参数更改绘图的限制:

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")

plot_zoom

or you can subset shp (an object of class SpatialPolygonsDataFrame ): 或者您可以子集shp (类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")

plot_subset

Applied Spatial Data Analysis with R is a good reference for basic R usage and advanced spatial statistics. 使用R的应用空间数据分析是基本R使用和高级空间统计的良好参考。

Too many questions in there really. 那里的问题太多了。

First, read the R Spatial Task View for info on spatial data in R. 首先,阅读R Spatial任务视图,了解R中空间数据的信息。

Then maybe read an introduction to spatial data in R by me: http://www.maths.lancs.ac.uk/~rowlings/Teaching/UseR2012/introductionTalk.html 然后我可以阅读R中对空间数据的介绍: 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM