如何使用 ggplot2 和 sf 包绘制南向上的地图?

问题描述 投票:0回答:2

我正在尝试使用 ggplot2 和 sf 包绘制一张南向上的地图。

我尝试了

coord_flip
scale_y_reverse
的功能,但没有成功。

我使用的基本代码是:

library(tidyverse)
library(sf)
library(rnaturalearth)
library(rnaturalearthhires)

BRA <- ne_states(country = "Brazil",
                 returnclass = "sf")

ggplot(BRA) +
  geom_sf(fill = "white") +
  coord_sf() 

提前谢谢您。

r ggplot2 gis r-sf rnaturalearth
2个回答
3
投票

您可以尝试倒墨卡托投影:

ggplot(BRA) +
  geom_sf(fill = "white") +
  coord_sf(crs = paste0('+proj=merc +lat_0=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 ',
                         '+axis=wsu +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 ',
                         '+units=m +no_defs'))


1
投票

您必须在绘制数据之前修改数据。

第一步是创建一个旋转数据的函数。

rot = function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)

然后将其应用到您的数据集(使用 st_geometry())。

a <- BRA |> st_geometry() * rot(pi)

输出的是巴西地图旋转180°:

ggplot(a) +
geom_sf(fill = "white")

enter image description here

同样可以应用于您的数据点。请参阅仿射变换部分中的https://r-spatial.github.io/sf/articles/sf3.html

© www.soinside.com 2019 - 2024. All rights reserved.