2012-06-15 14 views
7

私は完全な初心者です。私は正常に境界マップをプロットする次のコードを持っています。私は、data.frameストアをポイントとして追加したいと思います。 私はOpenStreetMapのドキュメントからこれを理解することができないため、事前に謝罪...以下のコード:OpenStreetMapを使用してdata.frameからポイントをプロットする

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パッケージを使って地図を取得して描画する方法もあります。 get_map関数は、さまざまなソース(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

おかげムーチョが...これは完璧に動作します! – JimmyT

関連する問題