使用OpenStreetMap从data.frame绘制点 [英] Plotting points from a data.frame using OpenStreetMap

查看:119
本文介绍了使用OpenStreetMap从data.frame绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位拥有完整空间数据的新手.我有以下代码可以成功绘制有界图.我想补充一点,data.frame存储. 我为无法从OpenStreetMap文档中找到答案而在此提前道歉...下面的代码:

I am a complete novice with spacial data. I have the following code that successfully plots a bounded map. I would like to add, as points the data.frame stores. I apologize in advance for not being able to figure this out from the OpenStreetMap documentation...code below:

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.

我怀疑商店的格式不适用于空间数据.

I suspect the format of stores isn't proper for spatial data.

推荐答案

我不知道OpenStreetMap软件包.但是我提供了一个替代方案,它仍然绘制OpenStreet地图,但使用ggmap包来获取和绘制地图. get_map函数可以从多种来源获取地图:osm,google,雄蕊和cloudmade;用source设置.此外,来源具有不同的样式,可通过maptype进行设置.地图的边界在位置向量中给出.或者,位置向量可以为地图的中心提供适当的缩放级别集.地图是用ggplot2绘制的,因此可以将点和标签添加到地图,就像将它们添加到任何ggplot对象一样.要运行以下程序,需要安装ggmapggplot2软件包.

I don't know the OpenStreetMap package. But I offer an alternative that still draws an OpenStreet Map, but uses the ggmap package to fetch and draw the map. The get_map function can fetch maps from a variety of sources: osm, google, stamen, and cloudmade; set with source. In addition the sources have different styles, set with maptype. The boundary of the map is given in a location vector. Alternatively, the location vector can give the centre of the map with an appropriate zoom level set. The map is drawn with ggplot2, and so points and labels can be added to the map as if they were being added to any ggplot object. To run the following, the ggmap and ggplot2 packages need to be installed.

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)

结果是:

标签可能会在后台丢失.在这种情况下,类似这样的方法可能是合适的.它使用此处的代码为文本提供轮廓.

The labels could be getting lost in the background. In that case, something like this might be appropriate. It uses code from here to give the text an outline.

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)

这篇关于使用OpenStreetMap从data.frame绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆