ggplot2::fortify的后继者

31
在最新的ggplot2版本中,?fortify返回:

描述

我现在推荐使用broom包,而不是使用此函数,因为后者实现了更广泛的方法。 fortify未来可能会被弃用。

broom包确实提供了许多替代方案(如augment)。应在何种情况下使用哪个替代方案?
我特别关心的是fortify(spdf)的替代方案,其中spdf是SpatialPolygonsDataFrame。
3个回答

32

这是我处理这个主题的方法。

在搜索了“broom cran”后,我被重定向到CRAN软件包的相应页面。它提供了一些vignettes,所以我查看了Introduction to broom。在未找到任何与“spatial”匹配的字符串后,我关闭了PDF并打开了参考手册。搜索“spatial”,我得到了7个匹配项,其中第一个是关于sp_tidiers主题的。函数tidy被宣传为将空间对象转换为data.frame。让我们试试吧。

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

14

发布这篇文章仅仅是为了展示tidy版本与fortify版本非常相似,甚至在tidy文档中都有提到:

这些函数最初来自于ggplot2包中的"fortify"函数。

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
}

我说“近似”,因为在 tidy 实现中微妙的差异(与 fortify 相比)导致生成的 data.frame 列的顺序不同。 因此,它们具有与 fortify 版本在较大空间对象上一样的“缓慢性”包袱,并且没有令人信服的理由去切换(在我看来),直到 fortify 被弃用。


1
有没有简单/直接的方法来改善实现?(我问这个问题是因为 broom,与 fortify 不同,正在积极开发中 ...) - Ben Bolker
1
关于这个引用,值得注意的是 broom 手册也对 augment 说了同样的话,这也是引发这个问题的原因。 - Hugh

0
作为一般性的回答,augment() 产生与 fortify 相同的输出。不同之处在于它返回一个 tibble 而不是数据框。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接