使用ggmap,geom_point和循环映射long-lat数据集的最近邻居

时间:2015-04-12 08:37:43

标签: r ggplot2 geospatial subset ggmap

我的最终目标是使用ggplot2包中的geom_path将一组建筑物的所有最近邻居(基于欧几里德距离)连接到ggmap上。我需要一个循环的帮助,这将允许我尽可能容易地绘制所有邻居

我在北京的3种建筑物之间以公里为单位创建了一个距离矩阵(称为' kmnew'):B(x2),D(x2)和L(x1):

   B        B        D        D        L
B NA 6.599014 5.758531 6.285787 3.770175
B NA       NA 7.141096 3.873296 5.092667
D NA       NA       NA 3.690725 2.563017
D NA       NA       NA       NA 2.832083
L NA       NA       NA       NA       NA

我尝试通过声明矩阵并使用循环来确定最近的邻居建筑来逐行辨别每个建筑物的最近邻居:

nn <- matrix(NA,nrow=5,ncol=1)


for (i in 1:nrow(kmnew)){
  nn[i,] <- which.min(kmnew[i,]) 
}

这会返回以下错误(不确定原因):

Error in nn[i, ] <- which.min(kmnew[i, ]) : replacement has length zero

但似乎回复了nn的正确答案:

     [,1]
[1,]    5
[2,]    4
[3,]    5
[4,]    5
[5,]   NA

我将其附加到名为newbjdata的原始数据框:

colbj <- cbind(newbjdata,nn)

返回

  Name Store sqft     long      lat nn
1    B     1 1200 116.4579 39.93921  5
2    B     2  750 116.3811 39.93312  4
3    D     1  550 116.4417 39.88882  5
4    D     2  600 116.4022 39.90222  5
5    L     1 1000 116.4333 39.91100 NA

然后我通过ggmap检索我的地图:

bjgmap <- get_map(location = c(lon = 116.407395,lat = 39.904211),
                  zoom = 13, scale = "auto",
                  maptype = "roadmap",
                  messaging = FALSE, urlonly = FALSE,
                  filename = "ggmaptemp", crop = TRUE,
                  color = "bw",
                  source = "google", api_key)

我的最终目标是使用ggplot包中的geom_path将最近邻居映射到一起。

例如,B型(第1行)的第1建筑物的nn是L型(第5行)的1栋建筑物。显然,我可以通过对数据帧的所述2行进行子集来绘制这条线:

ggmap(bjgmap) +
geom_point(data = colbj, aes(x = long,y = lat, fill = factor(Name)),
           size =10, pch = 21, col = "white") +
geom_path(data = subset(colbj[c(1,5),]), aes(x = long,y = lat),col = "black")

但是,我需要一个像循环一样工作的解决方案,而且我无法弄清楚如何实现这一点,因为我需要引用nn列并将其再次引用回长数据n次。我完全相信我没有使用最有效的方法,因此我愿意接受替代方案。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

这是我的尝试。我使用gcIntermediate()包中的geosphere来设置行。首先,我需要重新排列您的数据。当您使用gcIntermediate()时,您需要出发和到达长/拉。那就是你需要四列。为了以这种方式安排您的数据,我使用了dplyr包。 mutate_each(colbj, funs(.[nn]), vars = long:lat)可以帮助您获得所需的到达时间/纬度。 .代表'long'和'lat'。 [nn]是变量的向量索引。然后,我使用了gcIntermediate()。这会创建SpatialLines。您需要将对象设为SpatialLinesDataFrame。然后,您需要将输出转换为“正常”data.frame。此步骤至关重要,以便ggplot可以读取您的数据。 fortify()正在做这项工作。

library(ggmap)
library(geosphere)
library(dplyr)
library(ggplot2)

### Arrange the data: set up departure and arrival long/lat

mutate_each(colbj, funs(.[nn]), vars = long:lat) %>%
rename(arr_long = vars1, arr_lat = vars2) %>%
filter(complete.cases(nn)) -> mydf

### Get line information

rts <- gcIntermediate(mydf[,c("long", "lat")],
                      mydf[,c("arr_long", "arr_lat")],
                      50,
                      breakAtDateLine = FALSE,
                      addStartEnd = TRUE,
                      sp = TRUE)

### Convert the routes to a data frame for ggplot use

rts <- as(rts, "SpatialLinesDataFrame")
rts.df <- fortify(rts)


### Get a map (borrowing the OP's code)                   
bjgmap <- get_map(location = c(lon = 116.407395,lat = 39.904211),
                  zoom = 13, scale = "auto",
                  maptype = "roadmap",
                  messaging = FALSE, urlonly = FALSE,
                  filename = "ggmaptemp", crop = TRUE,
                  color = "bw",
                  source = "google", api_key)

# Draw the map
ggmap(bjgmap) +
geom_point(data = colbj,aes(x = long, y = lat, fill = factor(Name)),
           size = 10,pch = 21, col = "white") +
geom_path(data = rts.df, aes(x = long, y = lat, group = group),
          col = "black")

enter image description here

修改

如果您想在一个序列中进行所有数据操作,以下是一种方法。 foo与上面的rts.df相同。

mutate_each(colbj, funs(.[nn]), vars = long:lat) %>%
rename(arr_long = vars1, arr_lat = vars2) %>%
filter(complete.cases(nn)) %>%
do(fortify(as(gcIntermediate(.[,c("long", "lat")],
                          .[,c("arr_long", "arr_lat")],
                          50,
                          breakAtDateLine = FALSE,
                          addStartEnd = TRUE,
                          sp = TRUE), "SpatialLinesDataFrame"))) -> foo

identical(rts.df, foo)
#[1] TRUE

DATA

colbj <- structure(list(Name = structure(c(1L, 1L, 2L, 2L, 3L), .Label = c("B", 
"D", "L"), class = "factor"), Store = c(1L, 2L, 1L, 2L, 1L), 
sqft = c(1200L, 750L, 550L, 600L, 1000L), long = c(116.4579, 
116.3811, 116.4417, 116.4022, 116.4333), lat = c(39.93921, 
39.93312, 39.88882, 39.90222, 39.911), nn = c(5L, 4L, 5L, 
5L, NA)), .Names = c("Name", "Store", "sqft", "long", "lat", 
"nn"), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5"))