ggplot2的fortify函数出现错误

6

我正在尝试使用ggplot2包中的fortify函数将空间对象转换为data.frame。但是我遇到了错误。例如,遵循Hadley Wickhan绘制多边形形状文件的示例中使用的完全相同的代码,我输入以下命令:

 require("rgdal") 
 require("maptools")
 require("ggplot2")
 require("plyr")
 utah = readOGR(dsn="/path/to/shapefile", layer="eco_l3_ut")
   OGR data source with driver: ESRI Shapefile 
   Source: ".", layer: "eco_l3_ut"
   with 10 features and 7 fields
   Feature type: wkbPolygon with 2 dimensions
 utah@data$id = rownames(utah@data)

一切似乎都正常工作:

 > str(utah)
      ..@ data       :'data.frame': 10 obs. of  8 variables:
      .. ..$ AREA      : num [1:10] 1.42e+11 1.33e+11 3.10e+11 4.47e+10 1.26e+11 ...
      .. ..$ PERIMETER : num [1:10] 4211300 3689180 4412500 2722190 3388270 ...
      .. ..$ USECO_    : int [1:10] 164 170 204 208 247 367 373 386 409 411
      .. ..$ USECO_ID  : int [1:10] 163 216 201 206 245 366 372 385 408 410
      .. ..$ ECO       : Factor w/ 7 levels "13","14","18",..: 7 3 1 4 5 6 2 4 4 6
      .. ..$ LEVEL3    : int [1:10] 80 18 13 19 20 21 14 19 19 21
      .. ..$ LEVEL3_NAM: Factor w/ 7 levels "Central Basin and Range",..: 4 7 1 6 2 5 3 6 6 5
      .. ..$ id        : chr [1:10] "0" "1" "2" "3" ...
      ...
      ...

然而,当我尝试使用ggplot2包中的函数fortify将utah对象转换时,会出现以下错误:

 > utah.points = fortify(utah, region="id")
  Error in UseMethod("fortify") : no applicable method for 'fortify' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial')"

我尝试使用fortify将所有其他空间对象转换时遇到了相同的错误;即使使用过去有效的代码(在升级到R版本3.0.2之前),仍然会出现这个问题。

我运行的是R版本3.0.2,使用Mac电脑,具有Intel Core i7和16GB内存。

3个回答

2

我遇到了同样的问题。
重新安装主要包之后,错误信息仍然存在。

最终我意识到,在加载 ggplot2 之后,包 lme4 中也存在一个名为 fortify 的函数。
使用 ggplot2::fortify(utah, region="id") 解决了这个问题。


1
我意识到问题与.Rprofile文件有关。这是我在该文件中的内容:
options(repos="http://cran.stat.ucla.edu")
utils::update.packages(ask=FALSE)
pkgs <- getOption("defaultPackages")
options(defaultPackages = c(pkgs,"ggplot2","arm", "Zelig","stringr", "plyr", "reshape2", "MatchIt", "ISLR", "rgdal"))

每当从.Rprofile加载ggplot2时,我会收到上面提到的错误。当我从.Rprofile选项中删除ggplot2时,就不会出现错误。

0

我下载了shapefile并尝试了您的代码;它(或者更确切地说是forfify)在我的安装上似乎运行良好。我建议您重新安装主要软件包,重新启动并再次尝试。

> utah.points = fortify(utah, region="id")
Loading required package: rgeos
rgeos version: 0.3-1, (SVN revision 413M)
 GEOS runtime version: 3.3.8-CAPI-1.7.8 
 Polygon checking: TRUE 

> head(utah.points)
      long     lat order  hole piece group id
1 -1405382 2224519     1 FALSE     1   0.1  0
2 -1406958 2222744     2 FALSE     1   0.1  0
3 -1408174 2221195     3 FALSE     1   0.1  0
4 -1409680 2220162     4 FALSE     1   0.1  0
5 -1411068 2219579     5 FALSE     1   0.1  0
6 -1412780 2219001     6 FALSE     1   0.1  0
> tail(utah.points)
          long     lat order  hole piece group id
19615 -1172872 1741373 19615 FALSE     1   9.1  9
19616 -1172522 1740139 19616 FALSE     1   9.1  9
19617 -1172366 1739158 19617 FALSE     1   9.1  9
19618 -1172124 1737840 19618 FALSE     1   9.1  9
19619 -1171788 1737281 19619 FALSE     1   9.1  9
19620 -1171309 1736884 19620 FALSE     1   9.1  9
> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接