r - 更改 r 中 sf 对象的经度和纬度值
问题描述
我是新手sf
。在下面的代码中,我生成了两张地图,一张用于美国,一张用于澳大利亚。我想把这两个放在一起ggplot
。longitude
我试图latitude
在geometry
. 我只是想知道是否有一种快速的方法可以做到这一点。任何建议,将不胜感激。
library(tidyverse)
library(sf)
library(rnaturalearth)
map_au <-ne_states(country = c("australia"), returnclass ="sf") %>%
select(state = name, geometry)
map_us <-ne_states(country = c("united states of america"), returnclass ="sf") %>%
select(state = name, geometry) %>%
filter(!state %in% c("Alaska", "Hawaii"))
ggplot(data = map_us, aes(fill = state))+
geom_sf()+
geom_sf(data = map_au)+
theme(legend.position = "none")
由reprex 包于 2020-11-04 创建(v0.3.0)
解决方案
sf
允许您对几何图形进行任意仿射变换,包括平移。我们可以通过添加坐标向量或使用变换矩阵来移动几何图形(这里没有必要)。我们还需要替换对象的 CRS 以便再次绘制它。
请注意,这实际上是在平面上移动形状,这可能不是您想要做的。特别是,实际区域和距离没有保留(我不知道澳大利亚从北到南的纬度是否比美国大陆多代表更多米......)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(rnaturalearth)
map_au <- ne_states(country = c("australia"), returnclass = "sf") %>%
select(state = name, geometry)
map_us <- ne_states(country = c("united states of america"), returnclass = "sf") %>%
select(state = name, geometry) %>%
filter(!state %in% c("Alaska", "Hawaii"))
map_au_moved <- map_au
st_geometry(map_au_moved) <- st_geometry(map_au_moved) + c(-180, 60)
st_crs(map_au_moved) <- st_crs(map_au)
ggplot(data = map_us, aes(fill = state))+
geom_sf()+
geom_sf(data = map_au_moved)+
theme(legend.position = "none")
由reprex 包于 2020-11-03 创建(v0.3.0)
推荐阅读
- python - while循环同一个词
- javascript - 未捕获的类型错误:无法读取未定义的属性“长度”。Javascript
- typescript - deno 在应用程序代码中测试嵌入式测试:为什么不呢?
- .htaccess - 拒绝访问具有特定扩展名的文件
- github - Github Actions 在部署前替换字符串
- c++ - 为什么正方形的第二列没有打印在它的例外位置?
- mysql - MySQL 8.0.x 中令人困惑的 SELECT FOR UPDATE 行为
- javascript - 想要将元素的最小值和最大值存储到对象
- tensorflow - 绘制测试和训练集的损失与历元历史
- outlook - Outlook 日历会议邀请的深层链接(如 Mailto),如何设置提醒?