8

私は空間データの完全な初心者です。境界マップを正常にプロットする次のコードがあります。data.frameが格納するポイントとして追加したいと思います。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.

ストアの形式が空間データに適していないのではないかと思います。

4

2 に答える 2

10

OpenStreetMapパッケージがわかりません。ggmapしかし、私はまだOpenStreetマップを描画するが、パッケージを使用してマップをフェッチして描画する代替手段を提供します。このget_map関数は、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)

結果は次のとおりです。

ここに画像の説明を入力してください

ラベルがバックグラウンドで失われている可能性があります。その場合、このようなものが適切かもしれません。ここからのコードを使用して、テキストにアウトラインを付けます。

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)

ここに画像の説明を入力してください

于 2012-06-15T23:23:02.417 に答える
2

グーグルスタートエンドは彼らのAPIを使用するために請求情報を必要とするので、ここにOpenStreetMapsだけを使用する解決策があります。

サンプルデータ

library( OpenStreetMap )
library( ggplot2 )
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 <- OpenStreetMap::openmap( c( lat[1], lon[1] ), c( lat[2], lon[2] ),zoom = 15, 'osm')

OSMマップはメルカトル図法であり、ストア座標はlat-lonであるため、次のようになります。
方法1:マップをlatlonに
変換する方法2:座標をメルカトル図法に変換する

方法1-マップをlatlonに変換する

autoplot( OpenStreetMap::openproj( portland ) ) +
  geom_point( data = stores, aes( x = longitude, y = latitude, size = 5 ) ) +
  geom_text( data = stores, aes( x = longitude + .001, y = latitude, label = name ), hjust = 0 ) +
  theme( legend.position = "none" )

ここに画像の説明を入力してください

方法2-ポイントをメルカトルに変換する

stores2 <- as.data.frame( 
  OpenStreetMap::projectMercator( lat = stores$latitude, 
                                  long = stores$longitude ) 
  )
stores2 <- cbind( stores, stores2)
autoplot( portland ) +
  geom_point( data = stores2, aes( x = x, y = y, size = 5 ) ) +
  geom_text( data = stores2, aes( x = x + 100, y = y, label = name ), hjust = 0 ) +
  theme( legend.position = "none" )

ここに画像の説明を入力してください

于 2019-03-05T14:53:11.130 に答える