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

孤街醉人 提交于 2019-12-04 10:50:27

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

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