函数强化ggplot2时出错 [英] Error with function fortify of ggplot2

查看:89
本文介绍了函数强化ggplot2时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




错误in(function(classes,fdef,mtable) ):
无法找到函数'proj4string'进行签名''NULL''的继承方法



代码如下:

 >库(maptools)
> gpclibPermit()
> ;库(ggplot2)
>库(rgdal)
>库(rgeos)
>库(ggmap)
> brMap< - readShapePoly BRASIL.shp)
> brMapDF< - fortify(brMap)
#这实际上可以工作

#但这不是

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

(函数(类,fdef,mtable)中的错误:
无法找到函数'proj4string'的继承方法对于签名''NULL''

这种情况发生在我拥有的所有shapefile中,所以我试过在上面的代码中)与我在s中找到的shapefile tackoverflow 格式化ggplot2映射,数据为 https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit

  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 < - fortif y(brMap)




结果brMapDF的结构如下:

 'data.frame':9316 obs。 7个变量:
$ 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:带有37个等级1的因子, 2,3,4,...:1 1 1 1 1 1 1 1 1 1 ...
$组:81个等级的因子AC.1,AL.1 ,..:1 1 1 1 1 1 1 1 1 1 ...
$ id:chrACACACAC...


I am getting this error with the method fortify in ggplot2:

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

The code is the following:

> 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"’

This happens with all the shapefiles that I have, so I tried (in the code above) with a shapefile that I found in stackoverflow Format the ggplot2 map, the data is https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/edit

解决方案

It's a bit of a workaround, but if you duplicate the UF column as an id column as shown in the example wiki from my comments under data preparation, the defaults for fortify will use the first column in the spatial data frame to separate out the polygons accordingly while adding the names under the id column.

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)

The resulting structure for brMapDF is then:

'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" ...

这篇关于函数强化ggplot2时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆