更改r中sf对象的经纬度值
Change longitude and latitude values of sf objects in r
我是 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 创建
sf
允许您对几何图形执行 arbitrary affine transformations,包括平移。我们可以通过添加坐标向量或使用变换矩阵(此处不需要)来移动几何图形。我们还需要替换对象的 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 package (v0.3.0)
于 2020-11-03 创建
我是 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")
sf
允许您对几何图形执行 arbitrary affine transformations,包括平移。我们可以通过添加坐标向量或使用变换矩阵(此处不需要)来移动几何图形。我们还需要替换对象的 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 package (v0.3.0)
于 2020-11-03 创建