将强化的data.frame转换回sf对象 [英] convert a fortified data.frame back to sf object
问题描述
fiftystater软件包提供了一张包含夏威夷和阿拉斯加的美国详细地图,如下所示.对象五十状态已被强化,可与ggplot2一起使用.但是,我想使用geom_sf将其绘制为一个sf对象.
fiftystater package provides a great map of USA that has Hawaii and Alaska as insets below. The object fifty_states comes already fortified for use with ggplot2. However, I would like to plot this as an sf object using geom_sf.
作为一个更普遍的问题,将强化的data.frame转换回SF多边形的最佳方法是什么?
As a more general question, what is the best way to convert a fortified data.frame back into sf polygon?
library(fiftystater)
fifty_states <– fifty_states
> head(fifty_states)
long lat order hole piece id group
1 -85.07007 31.98070 1 FALSE 1 alabama Alabama.1
2 -85.11515 31.90742 2 FALSE 1 alabama Alabama.1
3 -85.13557 31.85488 3 FALSE 1 alabama Alabama.1
4 -85.13156 31.78381 4 FALSE 1 alabama Alabama.1
5 -85.13017 31.77885 5 FALSE 1 alabama Alabama.1
6 -85.11529 31.73157 6 FALSE 1 alabama Alabama.1
请注意此问题来自坐标的多边形有点相似,但不能完全满足我的需要
note this question polygons from coordinates is kind of similar but doesn't quite do what I need.
推荐答案
这将起作用:首先使用st_as_sf
转换为点sf
数据集,然后根据每个状态/块的点创建多边形.
this would work: you first convert to a point sf
dataset using st_as_sf
, and then create polygons from the points of each state/piece.
sf_fifty <- sf::st_as_sf(fifty_states, coords = c("long", "lat")) %>%
group_by(id, piece) %>%
summarize(do_union=FALSE) %>%
st_cast("POLYGON") %>%
ungroup()
plot(sf_fifty["id"])
(另请参见 https://github.com/r-spatial/sf/issues /321 )
这篇关于将强化的data.frame转换回sf对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!