我与空间数据的完整的新手。 我有以下代码,成功绘制了地图界。 我想补充,为点的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.
我怀疑专卖店的形式是不恰当的空间数据。
我不知道OpenStreetMap
包。 但是我提供的是仍然吸引了OpenStreet地图,而是使用一种替代ggmap
包来获取和绘制地图。 该get_map
函数可以从各种来源获取地图:OSM,谷歌,雄蕊,和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)
由于谷歌startend需要计费信息使用他们的API,这里仅仅是使用开放街道地图的解决方案。
样本数据
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的地图是在墨卡托,和店铺坐标是纬度,经度,那么:
方法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" )