我正在尝试使用 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()
提前谢谢您。
您必须在绘制数据之前修改数据。
第一步是创建一个旋转数据的函数。
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")
同样可以应用于您的数据点。请参阅仿射变换部分中的https://r-spatial.github.io/sf/articles/sf3.html。