2012-06-15 17 views
7

我是一個具有空間數據的完整新手。我有以下代碼成功地繪製了一個有界的地圖。我想補充一點,data.frame商店。 我提前道歉不能夠從OpenStreetMap的文檔想出解決辦法...下面的代碼:使用OpenStreetMap從數據框中繪製點

library(OpenStreetMap) 
stores <- data.frame(name=c("Commercial","Union","Bedford"), 
       longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037), 
       latitude=c(43.657471302616806,43.65663299041943,43.66091757424481)) 
lat <- c(43.68093,43.64278) 
lon <- c(-70.29548,-70.24097) 
portland <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=15,'osm') 
plot(portland,raster=TRUE) 
#can't figure out what to put here. 

我懷疑專賣店的形式是不恰當的空間數據。 在此先感謝, --JT

回答

12

我不知道OpenStreetMap包。但我提供了一個仍然繪製OpenStreet Map的替代方案,但使用ggmap包來獲取和繪製地圖。該功能可以從各種來源獲取地圖:osm,google,stamen和cloudmade;設置爲source。另外來源有不同的風格,設置maptype。地圖邊界在位置矢量中給出。或者,位置矢量可以爲地圖的中心設置適當的縮放級別。該地圖是用ggplot2繪製的,所以點和標籤可以添加到地圖,就好像它們被添加到任何ggplot對象一樣。要運行以下內容,需要安裝ggmapggplot2程序包。

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) 

結果是:

enter image description here

標籤可以在後臺迷路。在這種情況下,這樣的事情可能是合適的。它使用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) 

enter image description here

+0

感謝MUCHO ......這將很好地工作! – JimmyT