Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with function fortify of ggplot2

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.

like image 1000
Jose Antonio H. Company Avatar asked Oct 05 '13 13:10

Jose Antonio H. Company


Video Answer


2 Answers

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.

like image 102
Gilles Avatar answered Oct 06 '22 16:10

Gilles


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.

like image 38
Jose Antonio H. Company Avatar answered Oct 06 '22 15:10

Jose Antonio H. Company