【问题标题】:How rotate map in R如何在R中旋转地图
【发布时间】:2021-02-28 00:56:45
【问题描述】:

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

  • 不旋转图像

  • 旋转图像

我的初始代码没有旋转:

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)

【问题讨论】:

  • 见:elide | SO example
  • 看起来在map() 中有一个名为orientation 的选项可以让您执行此操作,但是当我以orientation = c(median(xlat), median(xlon), degrees = 45) 尝试时,我收到此错误消息:Error in plot && coord$error : invalid 'y' type in 'x && y'。跨度>

标签: r rotation


【解决方案1】:

通常当您调用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]);

【讨论】:

  • 确实不错(+1)。知道是否以及如何使用map 中的orientation 参数? “一个矢量c(latitude, longitude, rotation) 描述地图应该居中的位置以及围绕该中心顺时针旋转(以度为单位)。”听起来很有希望(正如上面@ulfelder 所指出的那样),至少对于像我这样无知的map 而言。
  • @Henrik 是的,在我开始使用三角函数解决方案之前,我注意到了这个参数,我得到了与@ulfelder 在他的评论中提到的相同的错误。看起来像函数中的错误。我只是尝试通过计算语言在我的 R 会话中修复它,我认为我成功了,但是我收到警告“某些数据的投影失败”,并且该图看起来与未指定 orientation 参数的外观相同。可能是一个失败的原因。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    如果您没有与 R 紧密集成,您可以使用 emptymap.js 探索地图旋转,这里是 the article 解释用法。

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

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2020-05-26
      相关资源
      最近更新 更多