在最新版本中ggplot2,?fortify返回:
描述
我现在推荐使用扫帚 包,而不是使用这个功能,它实现了更广泛的方法.
fortify可能会在将来弃用.
该broom软件包确实提供了许多替代方案(例如augment).在什么情况下应该使用哪一个?
我在替代特别感兴趣的fortify(spdf)地方spdf是一个SpatialPolygonsDataFrame.
Rom*_*rik 32
这是我接触这个主题的方式.
搜索"扫帚头"后,我被重定向到CRAN上相应的包裹页面.它提供了一些小插曲,所以我检查了扫帚简介.在找不到匹配"spatial"的任何字符串后,我关闭了PDF并打开了参考手册.搜索"空间"我得到了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
Run Code Online (Sandbox Code Playgroud)
hrb*_*str 14
发布此信息仅表明该tidy版本几乎与版本重复fortify它甚至在tidy文档中说明了:
这些函数起源于ggplot2包中的"fortify"函数.
broom:::tidy.SpatialPolygonsDataFramefunction (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
}
Run Code Online (Sandbox Code Playgroud)
ggplot2:::fortify.SpatialPolygonsDataFramefunction (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
}
Run Code Online (Sandbox Code Playgroud)
我说近了,因为实现中的微妙差异tidy(vs fortify)导致生成的data.frame列的顺序不同.因此,它们具有fortify版本在较大空间对象上所做的所有"缓慢"行李,并且在fortify弃用之前没有令人信服的理由进行切换(IMO).