我不知道OpenStreetMap
包。但我提供了一個仍然繪製OpenStreet Map的替代方案,但使用ggmap
包來獲取和繪製地圖。該功能可以從各種來源獲取地圖:osm,google,stamen和cloudmade;設置爲source
。另外來源有不同的風格,設置maptype
。地圖邊界在位置矢量中給出。或者,位置矢量可以爲地圖的中心設置適當的縮放級別。該地圖是用ggplot2
繪製的,所以點和標籤可以添加到地圖,就好像它們被添加到任何ggplot對象一樣。要運行以下內容,需要安裝ggmap
和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)
結果是:
標籤可以在後臺迷路。在這種情況下,這樣的事情可能是合適的。它使用code from here給文本提綱。
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)
感謝MUCHO ......這將很好地工作! – JimmyT