Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Successor to ggplot2::fortify

Tags:

r

ggplot2

broom

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.

like image 704
Hugh Avatar asked Dec 31 '15 08:12

Hugh


2 Answers

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
like image 128
Roman Luštrik Avatar answered Nov 20 '22 04:11

Roman Luštrik


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.

like image 14
hrbrmstr Avatar answered Nov 20 '22 05:11

hrbrmstr