【问题标题】:Successor to ggplot2::fortifyggplot2::fortify 的继任者
【发布时间】:2015-12-31 08:31:01
【问题描述】:

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

说明

我现在建议使用扫帚,而不是使用此功能 包,它实现了更广泛的方法。 fortify 可能 将来会被弃用。

broom 包确实提供了许多替代方案(例如augment)。什么情况下应该使用哪一种?

我对@9​​87654326@ 的替代方案特别感兴趣,其中spdf 是SpatialPolygonsDataFrame。

【问题讨论】:

    标签: r ggplot2 broom


    【解决方案1】:

    我是这样处理这个主题的。

    在搜索“broom cran”后,我被重定向到 CRAN 上相应的包裹页面。它提供了一些小插曲,所以我查看了 扫帚简介。在找不到任何匹配“空间”的字符串后,我关闭了 PDF 并打开了reference manual。搜索“空间”时,我得到了 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
    

    【讨论】:

      【解决方案2】:

      发布此内容只是为了表明tidy 版本与fortify 版本几乎相同 它甚至在tidy 文档中也说了这么多:

      这些函数源自 ggplot2 包中的“强化”函数。

      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 被弃用之前没有令人信服的理由切换(IMO)。

      【讨论】:

      • 是否有一种简单/直接的方法来改进实施? (我问是因为broomfortify 不同,它正在积极开发中......)
      • 关于引用,值得注意的是,broom 手册对augment 也有同样的说法,这就是导致这个问题的原因。
      【解决方案3】:

      作为一般答案,augment() 产生与fortify 相同的输出。不同之处在于它恢复的是 tibble 而不是数据框。

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多