31

In the most recent version of ggplot2, ?fortify returns:

Description

Rather than using this function, I now recomend using the broom package, which implements a much wider range of methods. fortify may be deprecated in the future.

The broom package indeed offers many alternatives (such as augment). Which one should be used in what circumstances?

I am particularly interested in an alternative to fortify(spdf) where spdf is a SpatialPolygonsDataFrame.

Hugh
  • 15,521
  • 12
  • 57
  • 100

3 Answers3

32

Here's how I approached the subject.

After searching "broom cran" I was redirected to the corresponding page of the package on CRAN. It offers a few vignettes so I checked out Introduction to broom. After failing to find any string matching "spatial" I closed the PDF and opened the reference manual. Searching for "spatial" I got 7 hits, with the first one on the topic of sp_tidiers. Function tidy is advertised to convert a spatial object to a data.frame. Let's try it.

library(sp)
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
x = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

library(broom)

tidy(x)

   long lat order  hole piece  group   id
1     2   2     1 FALSE     1   s1.1   s1
2     1   4     2 FALSE     1   s1.1   s1
3     4   5     3 FALSE     1   s1.1   s1
4     4   3     4 FALSE     1   s1.1   s1
5     2   2     5 FALSE     1   s1.1   s1
6     5   2     1 FALSE     1   s2.1   s2
7     2   2     2 FALSE     1   s2.1   s2
8     4   3     3 FALSE     1   s2.1   s2
9     5   2     4 FALSE     1   s2.1   s2
10    4   5     1 FALSE     1 s3/4.1 s3/4
11   10   5     2 FALSE     1 s3/4.1 s3/4
12    5   2     3 FALSE     1 s3/4.1 s3/4
13    4   3     4 FALSE     1 s3/4.1 s3/4
14    4   5     5 FALSE     1 s3/4.1 s3/4
15    5   4     6  TRUE     2 s3/4.2 s3/4
16    5   3     7  TRUE     2 s3/4.2 s3/4
17    6   3     8  TRUE     2 s3/4.2 s3/4
18    6   4     9  TRUE     2 s3/4.2 s3/4
19    5   4    10  TRUE     2 s3/4.2 s3/4
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
14

Posting this to solely show that the tidy version is near duplicate of the fortify version It even says as much in the tidy docs:

These functions originated in the ggplot2 package as "fortify" functions.

broom:::tidy.SpatialPolygonsDataFrame

function (x, region = NULL, ...) 
{
    attr <- as.data.frame(x)
    if (is.null(region)) {
        coords <- ldply(x@polygons, tidy)
        message("Regions defined for each Polygons")
    }
    else {
        cp <- sp::polygons(x)
        unioned <- maptools::unionSpatialPolygons(cp, attr[, 
            region])
        coords <- tidy(unioned)
        coords$order <- 1:nrow(coords)
    }
    coords
}

ggplot2:::fortify.SpatialPolygonsDataFrame

function (model, data, region = NULL, ...) 
{
    attr <- as.data.frame(model)
    if (is.null(region)) {
        coords <- plyr::ldply(model@polygons, fortify)
        message("Regions defined for each Polygons")
    }
    else {
        cp <- sp::polygons(model)
        unioned <- maptools::unionSpatialPolygons(cp, attr[, 
            region])
        coords <- fortify(unioned)
        coords$order <- 1:nrow(coords)
    }
    coords
}

I say near since the subtle differences in the tidy implementation (vs fortify) cause differences in the order of the generated data.frame columns. So, they have all of the "slowness" baggage the fortify version does on larger spatial objects and there's no compelling reason to switch (IMO) until fortify is deprecated.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • 1
    is there a simple/straightforward way to improve the implementation? (I ask because `broom`, unlike `fortify`, is under active development ...) – Ben Bolker Dec 31 '15 at 15:35
  • 1
    With regard to the quote, it's worth noting that the `broom` manual says the same thing about `augment`, which is what lead to this question. – Hugh Jan 02 '16 at 01:11
0

As a general answer, it is augment() that produces the same output as fortify. The difference lies in the fact that it restitues a tibble instead of a data frame.

Dario Lacan
  • 1,099
  • 1
  • 11
  • 25