简体   繁体   中英

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

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.

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM