Domanda

I am trying to convert a spatial object into a data.frame using the function fortify from the package ggplot2. But I am getting an error. For example, following the exact same code used in Hadley Wickhan's plotting polygon shapefiles example, I type the following line of commands:

 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)

Everything seems to work OK:

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

However, when I try to convert the utah object using the function fortify from the packcage ggplot2, I get the following error:

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

I am getting the same error for all other spatial objects that I have tried to convert using fortify; even when using code that had worked ok in the past (before upgrading to R's version 3.0.2).

I have R version 3.0.2 running on a Mac with Intel Core i7 and 16GB of ram.

È stato utile?

Soluzione 2

I realized that the problem has to do with the .Rprofile file. This is what I have in that file:

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

Whenever ggplot2 loads from .Rprofile, I get the error mentioned in my question above. Whenever I take out ggplot2 from the .Rprofile options, I don't get the error.

Altri suggerimenti

I got the same problem.
After reinstalling the main packages, the error message was still there.

Eventually I realized that a function fortify is also present in the package lme4 that was loaded after ggplot2.
Using ggplot2::fortify(utah, region="id") solved the problem.

I downloaded the shapefile and tried your code; it (or rather forfify) appeared to work fine on my installation. I suggest you reinstall the main packages, reboot and try again.

> 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
> 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top