AndMan21
AndMan21

Reputation: 533

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: enter image description here

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

enter image description here

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.

Upvotes: 5

Views: 1909

Answers (1)

Sandy Muspratt
Sandy Muspratt

Reputation: 32789

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

enter image description here

Upvotes: 3

Related Questions