r - 使用 OpenStreetMap 从 data.frame 中绘制点

标签 r openstreetmap ggmap

我是空间数据的完全新手。我有以下代码成功绘制了有界 map 。我想补充一点,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 Map 的替代方案,但使用了 ggmap用于获取和绘制 map 的包。 get_map函数可以从多种来源获取 map :osm、google、stamen 和 cloudmade;设置 source .另外源有不同的样式,设置为maptype . map 的边界在位置向量中给出。或者,位置向量可以给出具有适当缩放级别设置的 map 中心。 map 是用 ggplot2 绘制的,因此可以将点和标签添加到 map 中,就像将它们添加到任何 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

关于r - 使用 OpenStreetMap 从 data.frame 中绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056738/

相关文章:

r - Bass模型遗传算法的R实现

openstreetmap - osm立交桥按国家查询

r - 如何替换 ggmap 对象中的颜色?

r - 使用 ggmaps 创建黑白 map

r - data.table 为独特的观察赋值

r - 如何在 R 中将逗号和冒号分隔的列拆分为相应的列?

java - JMapViewer、MouseListener调用2次

尽管随用随付,但 r ggmap 地理编码查询超出了最大值?

r - 通过不同的公共(public)列组合迁移进出数据

openstreetmap - 在 OpenStreetMap 中绘制路径