简体   繁体   English

使用ggplot2错误绘制形状文件

[英]Plotting a shape file with ggplot2 error

Over the last few days I pretty much dove head first into using R for mapping. 在过去的几天里,我几乎首先使用R进行映射。 I have used R extensively for modelling etc. but not this kind of work before. 我已广泛使用R进行建模等,但之前没有这种工作。 I have some questions and issues regarding shapefiles, how they're read and so on. 我有一些关于shapefile的问题和问题,如何阅读等等。

I have downloaded the shape files from the Australian Bureau of Statistics there are numerous files with state borders, post codes, cities and so on. 我已经从澳大利亚统计局下载了形状文件,有许多文件,包括州边界,邮政编码,城市等。 The shape files are massive, the Australian state borders has about 1.8 million coordinate points in it, the other file I tried was the statistical area which has over 8 million in it. 形状文件很大,澳大利亚国家边界有大约180万个坐标点,我试过的另一个文件是统计区域,其中有超过800万个。 I didn't do anything with this file as it is just too big for my R set up. 我没有对这个文件做任何事情,因为它对我的R设置来说太大了。

I read the shape file in with readShapePoly and converted it like so 我用readShapePoly读取了shape文件并将其转换为如此

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

Once I had converted the State borders shape file from SpatialPolygonsDataFrame to a regular dataframe I plotted it successfully but it took forever and the detail was too great. 一旦我将州边界形状文件从SpatialPolygonsDataFrame转换为常规数据帧,我就成功地绘制了它,但它花了很SpatialPolygonsDataFrame ,细节太大了。 I thought to use thinnedSpatialPoly to simplify it but it gives the error: 我想使用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

Which google cannot help me with. 哪个谷歌无法帮助我。

My next strategy was to read it into SAS and use proc greduce which takes the file and creates a density field and you can choose how dense the polygons are. 我的下一个策略是将其读入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 has crap graphics and couldn't even plot the thing for me so I exported the dataset and read it back into R with the new density field which I hoping to subset the dataframe by and use in my plots. SAS有垃圾图形,甚至无法为我绘制图形,所以我导出了数据集并用新的密度字段将其读回到R中,我希望将数据框子集化并在我的图中使用。

My problem now is that when I go to plot in R i get this 我现在的问题是,当我去R的情节时,我得到了这个 澳大利亚国家边界地图

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

I guess it is because the polygons are not in order or have broken? 我想这是因为多边形不整齐或已经破碎? I used this function to try and rejoin my polygons but still no luck 我用这个函数尝试重新加入我的多边形,但仍然没有运气

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
}  

So, finally my questions! 所以,最后我的问题! How do people deal with large overly detailed shape files? 人们如何处理大量过于细致的形状文件? Why wasn't thinnedspatialpoly working (I'd like to avoid SAS if possible)? 为什么没有稀疏空间聚合工作(如果可能,我想避免使用SAS)? How can I get my plot to not look like crap? 我如何让我的阴谋看起来不像废话?

Finally my R specs: 最后我的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   

First, if you are diving in head first, don't go into the shallow end. 首先,如果你先潜入头部,不要进入浅水区。

My fairly old PC can read the state digital boundaries in shapefile format no problem: 我相当老的PC可以读取shapefile格式的状态数字边界没问题:

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

but the map is clearly over detailed. 但地图显然过于详细。 I also loaded it into Quantum GIS so I could have a good old zoom and pan around, and every tiny island is on there. 我还把它加载到Quantum GIS中,所以我可以有一个很好的旧缩放和平移,每个小岛都在那里。 I think its one of the most detailed country level maps I've ever seen. 我认为它是我见过的最详细的国家级地图之一。 So secondly, you might want to try and find a readily-simplified map of the states (see www.gadm.org for possibles). 其次,您可能想尝试找到一个易于简化的状态图(有关可能的信息,请参阅www.gadm.org)。

So lets see if gSimplify from package:rgeos helps: 那么让我们看看是否gSimplify来自包:rgeos帮助:

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

that removes a lot of the tiny islands but sadly for me (and a large chunk of the population) it removes New South Wales as well. 它消除了许多小岛屿,但遗憾的是我(以及大部分人口)它也移除了新南威尔士州。 Not good. 不好。 If I lower the tolerance eventually I can get something that keeps NSW: 如果我最终降低容忍度,我可以得到一些能够保持新南威尔士州

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

but clearly there's some issue with gSimplify or the shapefile data itself. 但显然gSimplify或shapefile数据本身存在一些问题。 Anyway, if I save aus2 back to a shapefile there's a massive reduction in size, the .shp being 180k instead of 29 megabytes. 无论如何,如果我将aus2保存回shapefile,那么大小会大幅缩小,.shp是180k而不是29兆。

Also, I'd stick to plotting with base graphics. 此外,我坚持用基本图形绘图。

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

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