2013-06-21 42 views
4

我收到此错误与方法强化在GGPLOT2:错误与功能强化的GGPLOT2

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string’ for signature ‘"NULL"’

的代码如下:

> library(maptools) 
> gpclibPermit() 
> library(ggplot2) 
> library(rgdal) 
> library(rgeos) 
> library(ggmap) 
> brMap <- readShapePoly("Google/BRASIL.shp") 
> brMapDF <- fortify(brMap) 
# This actually works 

# But this don´t 

> brMapDF <- fortify(brMap, region="UF") 

Error in (function (classes, fdef, mtable) : 
      unable to find an inherited method for function ‘proj4string’ for signature ‘"NULL"’ 

这种情况与所有的shape文件是我有,所以我尝试了(在上面的代码中)与我在shapeoverflow中找到的shapefile,我发现在stackoverflow Format the ggplot2 map,数据是https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit

回答

1

这是一种解决方法,但是如果您在数据准备工作中将我的评论中的示例wiki中所示的UF列作为id列复制,则fortify的默认值将使用空间数据框中的第一列来分隔出相应的多边形,同时在id列下添加名称。

library(maptools) 
library(ggplot2) 
library(sp) 
library(rgdal) 
library(rgeos) 

brMap <- readShapePoly("Google/BRASIL", IDvar = "UF", 
    proj4string = CRS("+init=epsg:4236"), repair = TRUE, verbose = TRUE) 
[email protected]$id <- [email protected]$UF 
brMapDF <- fortify(brMap) 

为brMapDF所产生的结构则是:

'data.frame': 9316 obs. of 7 variables: 
$ long : num -68.6 -68.7 -68.8 -68.8 -68.9 ... 
$ lat : num -11.1 -11.2 -11.2 -11.1 -11.1 ... 
$ order: int 1 2 3 4 5 6 7 8 9 10 ... 
$ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... 
$ piece: Factor w/ 37 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ group: Factor w/ 81 levels "AC.1","AL.1",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ id : chr "AC" "AC" "AC" "AC" ... 
+0

好极了!现在它工作:)谢谢...但是,你能解释我是哪个问题?为什么原始代码没有工作? – nanounanue

+0

@nanounanue:当我尝试强化readOGR读入的Shapefile和指定的region =“UF”时,形状文件上的拓扑出现错误,其中有2个重叠区域由错误消息判断。 #Error:TopologyException:在-48.588852991466581 -27.99543264943113处发现LINESTRING(-48.5843 -27.9514,-48.5889 -27.9956)与LINESTRING(-48.5889 -27.9944,-48.5844 -28.066)之间的非节点交集。 –