R ggplot2的功能增强出错

R ggplot2的功能增强出错,r,ggplot2,gis,R,Ggplot2,Gis,我使用ggplot2中的方法fortify得到此错误: (函数(类、fdef、mtable)中出错: 找不到签名“NULL”的函数“proj4string”的继承方法 代码如下: > library(maptools) > gpclibPermit() > library(ggplot2) > library(rgdal) > library(rgeos) > library(ggmap) > brMap <- readShapePoly("G

我使用ggplot2中的方法fortify得到此错误:


(函数(类、fdef、mtable)中出错:
找不到签名“NULL”的函数“proj4string”的继承方法

代码如下:

> 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"’
>库(maptools)
>gpclibPermit()
>图书馆(GG2)
>图书馆(rgdal)
>图书馆(rgeos)
>图书馆(ggmap)

>brMap brMapDF brMapDF这是一种解决方法,但如果您将UF列复制为id列,如示例wiki中“数据准备”下“我的评论”中所示,则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)
brMap@data$id <- brMap@data$UF
brMapDF <- fortify(brMap)

太好了!现在成功了:)谢谢你。。。但是,你能解释一下问题出在哪里吗?为什么原始代码不起作用?@nanounaue:shape文件的拓扑有一个错误,当我试图加强readOGR读取的shapefile和指定的region=“UF”时,根据错误消息判断,您有两个重叠区域#错误:拓扑异常:在-48.588852991466581-27.99543264943113处发现线串(-48.5843-27.9514,-48.5889-27.9956)和线串(-48.5889-27.9944,-48.5844-28.066)之间存在非节点交点。
'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" ...