How do you rotate the view of a map in ggmap?

帅比萌擦擦* 提交于 2020-01-01 11:54:08

问题


I am looking to rotate the view within the ggmap object from the default of up = true north, to a custom angle of my choosing, but can't find the option within ggmap or get_map. Currently, I have the following code:

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

Which produces:

I would like to rotate the image so that the main street shown (Person Street) is vertically-aligned, like this (which I just rotated manually in a screencapture software):

My goal, of course, is to still have a horizontal and vertical x and y axis as the original image, but have the actual "viewport" be rotated.


回答1:


Draw the map in a rotated (and re-sized) viewport, but rotate the labels in the opposite direction. The position (hjust, vjust) of the lon labels needs a little adjustment. If the angle of rotation isn't too big, the adjustment is okay.

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))



来源:https://stackoverflow.com/questions/26977511/how-do-you-rotate-the-view-of-a-map-in-ggmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!