我在ggplot2中使用fortify方法收到此错误:
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"’
我的所有shapefile都会发生这种情况,所以我尝试了(在上面的代码中)我在stackoverflow Format the ggplot2 map中找到的shapefile,数据为https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit
答案 0 :(得分:1)
这是一种解决方法,但是如果您根据数据准备中的注释将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)
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" ...