2012-07-11 88 views
8

在过去的几天里,我非常喜欢用R来绘制映射。我广泛使用R进行建模等,但之前没有这种工作。我有关于shapefile的一些问题和问题,它们是如何读取的等等。用ggplot2错误绘制形状文件

我已经从Australian Bureau of Statistics下载了形状文件,有很多文件有国家边界,邮政编码,城市等。 形状文件很大,澳大利亚国家边界内有大约180万个坐标点,我试过的另一个文件是其中有800多万个统计区域。我没有对这个文件做任何事情,因为它对于我的R设置来说太大了。

我与readShapePoly读取形文件,并转换它像这样

[email protected]$id = rownames([email protected])  
AUS.points = fortify(AUS, region="id") 
AUS.df = join(AUS.points, [email protected], by="id") 

有一次,我从SpatialPolygonsDataFrame转换的国家边界形状文件到正规的数据帧我绘制它成功,但它永远把与细节太棒了。 我想用thinnedSpatialPoly简化它,但它给人的错误:

Error in stopifnot(length([email protected]) == nrow(data)) :trying to get slot "polygons" from an object of a basic class ("NULL") with no slots 

其中谷歌不能帮助我。

我的下一个策略是将其读入SAS并使用proc greduce,该文件创建密度字段并可以选择多边形的密度。

proc mapimport out=states datafile='\Digital Boundaries\States\Shape file\STE_2011_AUST.shp'; 
id ste_code11; run; 
proc greduce data = states out = reduced_states; 
id ste_code11; run; 

SAS具有废话图形和甚至无法绘制事情对我来说,所以我导出的数据集,并与我希望通过给子集数据框,并在我的小区使用新的实地密度读回成R 。

我现在的问题是,当我去r跟踪我得到这个Map of Australian State Borders

ggplot(data=states.df, aes(X, Y, group=SEGMENT)) + 
geom_polygon(colour='black', fill='white') + theme_bw() 

我想这是因为多边形不按顺序排列或已经破裂?我用这个功能,试图重新加入我的多边形,但仍没有运气

RegroupElements <- function(df, longcol, idcol){ 
g <- rep(1, length(df[,longcol])) 
if (diff(range(df[,longcol])) > 300) { # check if longitude within group differs more    than 300 deg, ie if element was split 
d <- df[,longcol] > mean(range(df[,longcol])) # we use the mean to help us separate the extreme values 
g[!d] <- 1 # some marker for parts that stay in place (we cheat here a little, as we do not take into account concave polygons) 
g[d] <- 2 # parts that are moved 
} 
g <- paste(df[, idcol], g, sep=".") # attach to id to create unique group variable for the dataset 
df$group.regroup <- g 
df 
} 

### Function to close regrouped polygons 
# Takes dataframe, checks if 1st and last longitude value are the same, if not, inserts first as last and reassigns order variable 
ClosePolygons <- function(df, longcol, ordercol){ 
if (df[1,longcol] != df[nrow(df),longcol]) { 
tmp <- df[1,] 
df <- rbind(df,tmp) 
} 
o <- c(1: nrow(df)) # rassign the order variable 
df[,ordercol] <- o 
df 
} 

所以,最后我的问题! 人们如何处理大型过度细节的形状文件? 为什么不是thinnedpatialpoly工作(我想避免SAS,如果可能的话)? 我怎样才能让我的情节看起来不像垃圾?

最后我[R规格:

R version 2.15.1 (2012-06-22) 
Platform: x86_64-pc-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C      
[5] LC_TIME=English_Australia.1252  

attached base packages: 
[1] grid  stats  graphics grDevices utils  datasets methods 
[8] base  

other attached packages: 
[1] gridExtra_0.9 gpclib_1.5-1 ggmap_2.1  maptools_0.8-16 
[5] lattice_0.20-6 rgeos_0.2-7  plyr_1.7.1  stringr_0.6  
[9] ggplot2_0.9.1 sp_0.9-99  shapefiles_0.6 foreign_0.8-50 
[13] fastshp_0.1-0 

loaded via a namespace (and not attached): 
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2  labeling_0.1  
[5] MASS_7.3-18  memoise_0.1  munsell_0.3  png_0.1-4   
[9] proto_0.3-9.2  RColorBrewer_1.0-5 reshape2_1.2.1  RgoogleMaps_1.2.0 
[13] rjson_0.2.8  scales_0.2.1  tools_2.15.1 
+0

尝试'rgeos :: gSimplify'如果它适合你。 – 2012-07-11 08:20:01

+3

使用rgdal:readOGR来读取你的shape文件。为您的地图使用sp基础图形。 – Spacedman 2012-07-11 08:59:59

+0

另外,如果这些边界太详细,请尝试www.gadm.org中的那些 - 它们可能不是创作型的,但它们可能足以满足您的工作要求。 – Spacedman 2012-07-11 09:14:10

回答

8

首先,如果你是跳水头先不要进入浅水区。

我相当老的PC可以在shape文件格式读取的状态数字界限没有问题:

aus=readOGR(".","STE_2011_AUST") 
plot(aus) 

但地图显然在详细。我还将它加载到Quantum GIS中,这样我就可以拥有一个很好的缩放和平移,并且每个小岛都在那里。我认为它是我见过的最详细的国家级地图之一。其次,你可能想尝试找到一个简单的国家地图(见www.gadm.org可能)。

所以让我们看看gSimplify从包:rgeos帮助:

aus2 = gSimplify(aus,0.1) 
plot(aus2) 

,消除了大量的小岛,但不幸的是,我(和一大块的人口),它消除了新南威尔士州以及。不好。如果我降低公差最终我能得到的东西,保持NSW:

aus2 = gSimplify(aus,0.01) 
plot(aus2) 

但显然有一些问题与gSimplify或shape文件数据本身。无论如何,如果我将aus2保存到shape文件中,那么大小会有所减小,而.shp是180k而不是29MB。

此外,我会坚持绘制基本图形。

+0

感谢您的回答,我错误地认为您必须在绘制之前将shp文件转换为数据帧!当我有机会会自己使用'gSimplify' – user1414259 2012-07-11 23:40:32