更改r中sf对象的经度和纬度值

Zhi*_*ang 3 r ggplot2 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")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2020 年 11 月 4 日创建(v0.3.0)

Cal*_*You 7

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")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2020 年 11 月 3 日创建(v0.3.0)