使用ggmap对地图属性进行地理编码

时间:2017-06-21 07:15:51

标签: r igraph

我有一个igraph对象中每个顶点的邮政编码。我想使用ggmap将这些转换为地理坐标,这样我就可以计算出边缘属性=地理距离。

require(igraph)
require(ggmap)

g <- graph.ring(6)
V(grph)$postcode <- c("Johannesburg 2017", 
                      "Rondebosch 8000",
                      "Durban 4001", 
                      "Pietermaritzburg 3201", 
                      "Jeffreys Bay 6330", 
                      "Pretoria 0001" )

我以为我可以用这种方式为每个顶点生成地理坐标:

V(g)$coordinate <- geocode(V(g)$postcode, sensor = FALSE, 
                           output = "latlon", source = "google")

结果是针对每个顶点重复的所有顶点的latlon坐标列表,而不是每个顶点的唯一latlon。

head(head(V(g)$coordinate)
[[1]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[2]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

[[3]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[4]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

[[5]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[6]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

-ve numbers =纬度,+ ve数=经度。我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是geocode会返回一个数据框,但是当您将其分配给V(g)$coordinate时,它会将其视为一个列表,并循环使用这些列以获取每个顶点的值。

postcode_df <- geocode(V(g)$postcode, sensor = FALSE, 
                           output = "latlon", source = "google")

postcode_df
#        lon       lat
# 1 28.03837 -26.18825
# 2 28.31993 -25.84222
# 3 31.02204 -29.84962
# 4 30.36661 -29.65119
# 5 24.91015 -34.05067
# 6 28.18540 -25.74895

您需要将数据框的每一行转换为可以分配给顶点的元素。这可以通过很多方式完成,这里只是一个简单的方法:

V(g)$coordinate <- split(postcode_df, 1:nrow(postcode_df))

V(g)$coordinate
# [[1]]
# lon       lat
# 1 28.03837 -26.18825
# 
# [[2]]
# lon       lat
# 2 28.31993 -25.84222
# 
# [[3]]
# lon       lat
# 3 31.02204 -29.84962
# 
# [[4]]
# lon       lat
# 4 30.36661 -29.65119
# 
# [[5]]
# lon       lat
# 5 24.91015 -34.05067
# 
# [[6]]
# lon       lat
# 6 28.1854 -25.74895