1

我正在尝试使用 qmap(ggmap 的一部分)创建地图。这是一段示例代码,用于通过问题进行说明。这有点傻,但比使用我自己的数据更干净。

install.packages("ggmap")

library(ggmap)

qmap("Capitol Building, Washington DC", zoom = 15)

所以这是我的问题:我想放大国会大厦周围区域(zoom=15),以获得大量街道细节。但我也想在我的地图中加入华盛顿纪念碑。为此,我想扩展地图的西部并使其成为矩形。

有人知道该怎么做吗?任何见解将不胜感激。感谢您对初学者的耐心等待。

4

1 回答 1

2

Like this?

library(ggmap)
cap  <- geocode("Capitol Building, Washington DC")
wash <- geocode("Washington Monument, Washington DC")
loc  <- unlist((cap+wash)/2)
ggmap(get_map(location=loc,zoom=15))+coord_fixed(ylim=loc[2]+.005*c(-1,+1))

So this pulls in a map based on coords midway between the Capitol Building and the Washington Monument, then trims it by setting the ylim.

The reason for unlist(...) is that geocode(...) returns a data frame and get_map(...) wants a numeric vector.

EDIT Response to OP's comment.

coord_fixed(...) forces an aspect ratio of 1:1, meaning that 1° of latitude is the same length as 1° of longitude. To get back the original aspect ratio from the map, use coord_map(...).

ggmap(get_map(location=loc,zoom=15))+coord_map(ylim=loc[2]+.005*c(-1,+1))

于 2014-10-11T03:47:30.983 回答