简体   繁体   English

使用ggmap路线图和geom_polygon绘制缩放

[英]Plot zoom with ggmap roadmap and geom_polygon

I want to add county lines to a google roadmap from ggmap . 我想将县线从ggmap添加到Google路线图中。 I'm using the county lat/lon data from from ggplot2 . 我正在使用来自ggplot2的县纬度/经度数据。 The map is zoomed in to a smaller region than the entire county polygon. 地图被放大到比整个县多边形更小的区域。 Normally, when I am plotting a portion of a polygon, I would generate the whole polygon and then zoom in via coord_map 通常,当我绘制多边形的一部分时,我将生成整个多边形,然后通过coord_map进行放大

The problem here is that the google roadmap does not resize. 这里的问题是Google路线图没有调整大小。

library(tidyverse)
library(ggmap)

king <- map_data('county') %>% filter(region == 'washington', subregion == 'king')


bbox <- ggmap::make_bbox(lon = king$long, lat = king$lat, f = 0)
county_map <- ggmap::get_map(location = bbox, maptype = "roadmap", source = "google", zoom = calc_zoom(bbox) - 1)
county_map <- ggmap::ggmap(county_map)

c_map <- county_map + geom_polygon(data = king, aes(x = long, y = lat), fill = NA, color = 'blue')


c_map + coord_map(xlim = c(-122.4, -122),ylim = c(47.5, 47.9))

在此处输入图片说明

If I instead get the zoomed in roadmap from the start, I run into the problem of the polygon getting clipped and scrambled. 相反,如果我从一开始就获得了放大的路线图,则会遇到多边形被剪切和打乱的问题。

bbox2 <- ggmap::make_bbox(lon = c(-122.4, -122), lat = c(47.5, 47.9), f = 0)
county_map2 <- ggmap::get_map(location = bbox2, maptype = "roadmap", source = "google", zoom = calc_zoom(bbox2) - 1)
county_map2 <- ggmap::ggmap(county_map2) + coord_map(xlim = c(-122.4, -122),ylim = c(47.5, 47.9))

county_map2 + geom_polygon(data = king, aes(x = long, y = lat), fill = NA, color = 'blue')

在此处输入图片说明

So I'm trying to get the county line from the top plot onto the bottom plot. 因此,我正在尝试将县线从顶部绘制到底部绘制。 Thanks! 谢谢!

** Edit: Duplicate of linked post **编辑:链接文章重复

The post I flagged as a duplicate does seem to answer your question. 我标记为重复的帖子似乎确实回答了您的问题。

ggmap(county_map2, base_layer=ggplot(aes(x=long,y=lat), data=king),
      extent = "normal", maprange=FALSE) +
  geom_polygon(aes(x = long, y = lat),
               data = king, colour = 'blue', fill = NA, alpha = .5) +
  coord_map(projection="mercator", 
            xlim=c(attr(county_map2, "bb")$ll.lon, attr(county_map2, "bb")$ur.lon),
            ylim=c(attr(county_map2, "bb")$ll.lat, attr(county_map2, "bb")$ur.lat)) +
  theme_nothing()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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