繁体   English   中英

使用ggplot2错误绘制形状文件

[英]Plotting a shape file with ggplot2 error

在过去的几天里,我几乎首先使用R进行映射。 我已广泛使用R进行建模等,但之前没有这种工作。 我有一些关于shapefile的问题和问题,如何阅读等等。

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

我用readShapePoly读取了shape文件并将其转换为如此

AUS@data$id = rownames(AUS@data)    
AUS.points = fortify(AUS, region="id")
AUS.df = join(AUS.points, AUS@data, by="id")

一旦我将州边界形状文件从SpatialPolygonsDataFrame转换为常规数据帧,我就成功地绘制了它,但它花了很SpatialPolygonsDataFrame ,细节太大了。 我想使用thinnedSpatialPoly来简化它,但它给出了错误:

Error in stopifnot(length(Sr@polygons) == 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的情节时,我得到了这个 澳大利亚国家边界地图

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
}  

所以,最后我的问题! 人们如何处理大量过于细致的形状文件? 为什么没有稀疏空间聚合工作(如果可能,我想避免使用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   

首先,如果你先潜入头部,不要进入浅水区。

我相当老的PC可以读取shapefile格式的状态数字边界没问题:

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

但地图显然过于详细。 我还把它加载到Quantum GIS中,所以我可以有一个很好的旧缩放和平移,每个小岛都在那里。 我认为它是我见过的最详细的国家级地图之一。 其次,您可能想尝试找到一个易于简化的状态图(有关可能的信息,请参阅www.gadm.org)。

那么让我们看看是否gSimplify来自包:rgeos帮助:

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

它消除了许多小岛屿,但遗憾的是我(以及大部分人口)它也移除了新南威尔士州。 不好。 如果我最终降低容忍度,我可以得到一些能够保持新南威尔士州

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

但显然gSimplify或shapefile数据本身存在一些问题。 无论如何,如果我将aus2保存回shapefile,那么大小会大幅缩小,.shp是180k而不是29兆。

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

暂无
暂无

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

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