现在是 2021 年,使用现代的 sf R 包要容易得多。有了这个,您可以在绘制之前旋转底层多边形以获得所需的效果。这也通过rnaturalearth 包使用来自Natural Earth 的国家多边形数据。
library(sf)
library(rnaturalearth)
library(ggplot2)
# Rotate an sf geom around a center point. If no center is
# specified then it rotates around the center of the geom.
# This is technically an affine transformation: https://r-spatial.github.io/sf/articles/sf3.html#affine-transformations-1
st_ellide_rotate = function(x, degrees, center_coords=NULL){
if(degrees < -360 | degrees > 360) stop('Degrees must be in the range -360 to 360')
x = sf::st_combine(x)
if(is.null(center_coords)){
center_coords = sf::st_centroid(x)
}
radians = degrees * pi/180
transform_matrix = matrix(c(cos(radians), sin(radians), -sin(radians), cos(radians)), 2, 2)
return((x-center_coords) * transform_matrix + center_coords)
}
countries = rnaturalearth::ne_countries(scale = 10,returnclass = 'sf')
saved_crs = st_crs(countries)
points = st_sf(name=c('point1'), geometry = st_sfc(st_point(c(2,37)), crs = saved_crs))
countries_rotated = countries %>%
st_ellide_rotate(-20, center_coords = c(2,37))
# applying an affine transformation nulls the CRS for some reason, so reset it here
st_crs(countries_rotated) <- saved_crs
ggplot() +
geom_sf(data=countries_rotated) +
geom_sf(data=points, size=1) +
geom_sf_label(data=points, aes(label=name), nudge_x=1) +
coord_sf(xlim = c(-1,7), ylim=c(34,42)) +
labs(subtitle = 'rotated 20 deg')
ggplot() +
geom_sf(data=countries) +
geom_sf(data=points, size=1) +
geom_sf_label(data=points, aes(label=name), nudge_x = 1) +
coord_sf(xlim = c(-1,7), ylim=c(34,42)) +
labs(subtitle = 'original')