在过去的几天里,我非常喜欢用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跟踪我得到这个
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
尝试'rgeos :: gSimplify'如果它适合你。 – 2012-07-11 08:20:01
使用rgdal:readOGR来读取你的shape文件。为您的地图使用sp基础图形。 – Spacedman 2012-07-11 08:59:59
另外,如果这些边界太详细,请尝试www.gadm.org中的那些 - 它们可能不是创作型的,但它们可能足以满足您的工作要求。 – Spacedman 2012-07-11 09:14:10