我在 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 中找到的 shapefile设置 ggplot2 地图格式 https://stackoverflow.com/questions/15315939/format-the-ggplot2-map,数据为https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit
这是一个解决方法,但如果您将 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" ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)