如何在 R 中旋转地图 [英] How rotate map in R
问题描述
我只想旋转地图或更改方向,例如 45 度,而不是其他元素.有可能吗?例如:
I would like to rotate only the map, or change the orientation, for example 45 degrees, but not the other elements. It's posible? For example:
- 不旋转图像
- 旋转图像
我没有旋转的初始代码:
My initial code without rotating:
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)
推荐答案
通常当您调用 maps::map()
时,它会在函数调用期间自动绘制地图,但您可以通过 plot=F
来防止这种情况.同时,您可以将调用的返回值存储在一个变量中,该变量将包含所请求地图轮廓的 x 和 y 坐标.然后,您可以使用一些三角函数围绕中心点旋转所有 x 和 y 坐标,最后使用基本 R 绘图函数手动绘制旋转点.
Normally when you call maps::map()
it automatically plots the map during the function call, but you can pass plot=F
to prevent that. At the same time, you can store the return value from the call in a variable, which will contain the x and y coordinates of the contours of the requested map. You can then use some trigonometry to rotate all the x and y coordinates about a center point, and finally plot the rotated points manually using base R plotting functions.
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]);
这篇关于如何在 R 中旋转地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!