使用OpenStreetMap从data.frame打印点
我是一个完全的空间数据新手。下面的代码成功地绘制了一个有界映射。我想添加data.frame存储的点。 我为无法从OpenStreetMap文档中找出这一点提前道歉…代码如下:使用OpenStreetMap从data.frame打印点,r,openstreetmap,ggmap,R,Openstreetmap,Ggmap,我是一个完全的空间数据新手。下面的代码成功地绘制了一个有界映射。我想添加data.frame存储的点。 我为无法从OpenStreetMap文档中找出这一点提前道歉…代码如下: library(OpenStreetMap) stores <- data.frame(name=c("Commercial","Union","Bedford"), longitude=c(-70.25042295455933,-70.26050806045532,-70.277
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)
存储我不知道OpenStreetMap
包。但是我提供了一个替代方案,它仍然绘制OpenStreet地图,但是使用ggmap
包获取并绘制地图。get_map
函数可以从多种来源获取地图:osm、google、stamen和CloudMake;设置为源代码
。此外,源具有不同的样式,使用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)
库(ggmap)
商店由于谷歌startend需要使用其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')
方法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" )
stores2
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" )
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" )