簡體   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