我不知道OpenStreetMap
包裹。但我提供了一种替代方案,仍然绘制 OpenStreet Map,但使用ggmap
包来获取和绘制地图。这get_map
函数可以从多种来源获取地图:osm、google、stamen 和 cloudmade;设置为source
。此外,源有不同的样式,设置为maptype
。地图的边界以位置向量给出。或者,位置向量可以给出具有适当缩放级别集的地图中心。地图是用ggplot2
,因此可以将点和标签添加到地图中,就像将它们添加到任何 ggplot 对象中一样。要运行以下命令,ggmap
and ggplot2
需要安装软件包。
library(ggmap)
stores <- data.frame(name=c("Commercial","Union","Bedford"),
longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037),
latitude=c(43.657471302616806,43.65663299041943,43.66091757424481))
location = c(-70.2954, 43.64278, -70.2350, 43.68093)
# Fetch the map
portland = get_map(location = location, source = "osm")
# Draw the map
portlandMap = ggmap(portland)
# Add the points layer
portlandMap = portlandMap + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5)
# Add the labels
portlandMap + geom_text(data = stores, aes(label = name, x = longitude+.001, y = latitude), hjust = 0)
结果是:
标签可能会在背景中消失。在这种情况下,类似这样的事情可能是合适的。它用代码从这里给文本一个大纲。
portlandMap = ggmap(portland) + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5)
theta <- seq(pi/16, 2*pi, length.out=32)
xo <- diff(location[c(1,3)])/250
yo <- diff(location[c(2,4)])/250
for(i in theta) {
portlandMap <- portlandMap + geom_text(data = stores,
aes_(x = stores$longitude + .001 + cos(i) * xo,
y = stores$latitude + sin(i) * yo,
label = stores$name),
size = 5, colour = 'black', hjust = 0)
}
portlandMap +
geom_text(data = stores, aes(x = longitude + .001, y = latitude, label=name),
size = 5, colour = 'white', hjust = 0)