ggplot2::fortify 的继任者
Successor to ggplot2::fortify
最新版本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.
broom
包确实提供了很多选择(例如augment
)。什么情况下应该使用哪一种?
我对 fortify(spdf)
的替代方法特别感兴趣,其中 spdf
是一个 SpatialPolygonsDataFrame。
以下是我处理该主题的方式。
搜索 "broom cran" 后,我被重定向到 CRAN 上包的相应页面。它提供了一些小插曲,所以我查看了 扫帚简介。在找不到任何匹配 "spatial" 的字符串后,我关闭了 PDF 并打开了 reference manual。搜索 "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
发布此内容只是为了表明 tidy
版本与 fortify
版本几乎相同 它甚至在 tidy
文档中说了很多:
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
}
我说 near 因为 tidy
实现中的细微差别(与 fortify
相比)导致 order 生成的 data.frame 列。
因此,他们拥有 fortify
版本在较大空间对象上所做的所有 "slowness" 包袱,并且在 fortify
被弃用之前没有令人信服的理由切换(IMO)。
作为一般答案,augment()
产生与 fortify
相同的输出。区别在于它恢复的是小标题而不是数据框。
最新版本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.
broom
包确实提供了很多选择(例如augment
)。什么情况下应该使用哪一种?
我对 fortify(spdf)
的替代方法特别感兴趣,其中 spdf
是一个 SpatialPolygonsDataFrame。
以下是我处理该主题的方式。
搜索 "broom cran" 后,我被重定向到 CRAN 上包的相应页面。它提供了一些小插曲,所以我查看了 扫帚简介。在找不到任何匹配 "spatial" 的字符串后,我关闭了 PDF 并打开了 reference manual。搜索 "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
发布此内容只是为了表明 tidy
版本与 fortify
版本几乎相同 它甚至在 tidy
文档中说了很多:
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
}
我说 near 因为 tidy
实现中的细微差别(与 fortify
相比)导致 order 生成的 data.frame 列。
因此,他们拥有 fortify
版本在较大空间对象上所做的所有 "slowness" 包袱,并且在 fortify
被弃用之前没有令人信服的理由切换(IMO)。
作为一般答案,augment()
产生与 fortify
相同的输出。区别在于它恢复的是小标题而不是数据框。