首页 > 解决方案 > 更改 r 中 sf 对象的经度和纬度值

问题描述

我是新手sf。在下面的代码中,我生成了两张地图,一张用于美国,一张用于澳大利亚。我想把这两个放在一起ggplotlongitude我试图latitudegeometry. 我只是想知道是否有一种快速的方法可以做到这一点。任何建议,将不胜感激。

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)

标签: rggplot2sf

解决方案


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)


推荐阅读