如何在ggmap中旋转地图视图?

时间:2014-11-17 16:43:11

标签: r ggplot2 ggmap

我希望将ggmap对象中的视图从默认的up = true north旋转到我选择的自定义角度,但无法在ggmap或get_map中找到该选项。目前,我有以下代码:

map1 <- get_map(location=c(-78.872209, 35.050514), zoom = 17, maptype="hybrid")
ggmap(map1)

哪个产生: enter image description here

我想旋转图像,使显示的主要街道(人员街道)垂直对齐,就像这样(我只是在一个screencapture软件中手动旋转):

enter image description here

我的目标当然是将水平和垂直的x和y轴作为原始图像,但是具有实际的&#34;视口&#34;旋转。

1 个答案:

答案 0 :(得分:2)

在旋转(并重新调整大小)的视口中绘制地图,但以相反方向旋转标签。 lon标签的位置(hjust,vjust)需要稍微调整一下。如果旋转角度不是太大,调整就可以了。

library(ggmap)
library(grid)

# Get the map
lon <- c(165, 180)
lat <- c(-47.5, -33.5) 
map1 <- get_map(location = c(-78.872209, 35.050514), zoom = 17, maptype = "hybrid")

# Angle of rotation
rotate = -67

# Rotate the labels  in the opposite direction,
# plus an adjustment of the position of the tick mark labels 
map = ggmap(map1) +
      theme(axis.text.x = element_text(angle= -rotate, 
                            vjust = 1.1, hjust = ifelse(rotate > 0, 0, 1)),
            axis.text.y = element_text(angle = -rotate),
            axis.title.y = element_text(angle = -rotate, vjust = 0.5),
            axis.title.x = element_text(angle = -rotate))

# Draw the map in a rotated viewport, with its size adjusted
print(map, vp = viewport(width = .7, height = .7, angle = rotate))

enter image description here