功能强化ggplot2时出错

Jos*_*any 6 r shapefile ggplot2 fortify

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

 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)
Run Code Online (Sandbox Code Playgroud)

一切似乎都运作正常:

 > 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" ...
      ...
      ...
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用packcage ggplot2中的fortify函数转换犹他对象时,我收到以下错误:

 > 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')"
Run Code Online (Sandbox Code Playgroud)

对于我尝试使用fortify转换的所有其他空间对象,我得到了相同的错误; 即使使用过去运行正常的代码(在升级到R的3.0.2版之前).

我在配备Intel Core i7和16GB内存的Mac上运行R版3.0.2.

Jos*_*any 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"))
Run Code Online (Sandbox Code Playgroud)

每当 ggplot2 从 加载时.Rprofile,我都会收到上面问题中提到的错误。每当我从选项中取出 ggplot2 时.Rprofile,我都不会收到错误。