在R中更改sf对象的经纬度数值

3
我是sf的新手。在下面的代码中,我生成了两个地图,一个是美国的,另一个是澳大利亚的。我想将这两个地图并排放在同一个ggplot中。我尝试过更改geometry中澳大利亚的longitude和latitude值。我只是想知道是否有快速的方法来实现这一点。任何建议都将不胜感激。
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 package (v0.3.0)于2020年11月04日创建的

1个回答

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")

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


谢谢。这就是我想要的。对于我的目的来说已经足够好了。 - Zhiqiang Wang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接