如何在R中旋转地图

时间:2015-08-07 08:34:21

标签: r rotation

我想只旋转地图,或更改方向,例如45度,而不是其他元素。这是可能的吗? 例如:

  • 不旋转图像

enter image description here

  • 旋转图像

enter image description here

我的初始代码没有旋转:

library("maps")
library("mapproj")
library("mapdata")

xlon = seq(-1, 7, 0.01)
xlat = seq(34, 42, 0.01)

map(database = "worldHires",
    xlim = c(min(xlon), max(xlon)), ylim = c(min(xlat),max(xlat)),
    mar = c(0, 0, 0, 0))
text(2, 37, labels = "point1", pos = 4)
points(2, 37)

3 个答案:

答案 0 :(得分:5)

通常,当您致电maps::map()时,它会在函数调用期间自动绘制地图,但您可以通过plot=F来阻止它。同时,您可以将调用的返回值存储在变量中,该变量将包含所请求映射的轮廓的x和y坐标。然后,您可以使用一些三角法围绕中心点旋转所有x和y坐标,最后使用基本R绘图函数手动绘制旋转点。

library('maps');
library('mapproj');
library('mapdata');

xlon = seq(-1,7,0.01);
xlat = seq(34,42,0.01);

md <- map('worldHires',xlim=range(xlon),ylim=range(xlat),mar=c(0,0,0,0),plot=F);
md2 <- md;
rot <- -30*pi/180;
about <- c(2,37);
newangles <- atan2(md$y-about[2],md$x-about[1])+rot;
mags <- sqrt((md$x-about[1])^2+(md$y-about[2])^2);
md2$x <- about[1]+cos(newangles)*mags;
md2$y <- about[2]+sin(newangles)*mags;
par(mar=c(0,0,0,0)); plot(md2,type='l',xlim=range(xlon),ylim=range(xlat),axes=F,ann=F);

text(about[1],about[2],labels='point1',pos=4);
points(about[1],about[2]);

rotated

答案 1 :(得分:3)

现在是 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')

image rotated 20 degrees original image

答案 2 :(得分:1)

如果您没有与R紧密集成,可以使用emptymap.js探索地图轮换,此处the article说明用法。

免责声明:我是该模块的开发人员。