【问题标题】:SpatialLines to iGraph conversion simplifies topologySpatialLines 到 iGraph 的转换简化了拓扑
【发布时间】:2016-10-09 23:41:35
【问题描述】:

我正在尝试从 spatialLinesDataFrame 转换为 igraph 对象,并认为我可能会丢失我想要保留的信息。 igraph 相当新,所以请多多包涵。下面的例子说明:

# create sldf object
require(sp); require(igraph); require(shp2graph)
d = data.frame(x = c(0,80,100,0,-20,-8,0,3,-10,-5,80,75),
               y = c(0,-10,5,0,14,33,0,-4,-10,-12,-10,5),
               grp = c(1,1,1,2,2,2,3,3,3,3,4,4))

sl = SpatialLines(list(
  Lines(list(Line(d[d$grp == 1,1:2]),
             Line(d[d$grp == 4,1:2])), ID=1),
  Lines(Line(d[d$grp == 2,1:2]), ID=2),
  Lines(Line(d[d$grp == 3,1:2]), ID=3))
)

sldf = SpatialLinesDataFrame(sl, iris[1:3,])
plot(sldf)

现在转换为 igraph 并绘图:

read_sldf = readshpnw(sldf, ELComputed = T)
g = nel2igraph(read_sldf[[2]], read_sldf[[3]], weight=read_sldf[[4]])
plot(g)

第一个 spdf 行 (sldf[1,]) 的分支已丢失,我说得对吗?调用 as_edgelist(g) 返回 3 行而不是 4 行。

【问题讨论】:

  • 是的,完全正确。 sl2 = SpatialLines(list(Lines(Line(d[d$grp == 1,1:2]), ID=1), Lines(Line(d[d$grp == 2,1:2]), ID=2), Lines(Line(d[d$grp == 3,1:2]), ID=3))); sldf2 = SpatialLinesDataFrame(sl2, iris[1:3,]); read_sldf2 = readshpnw(sldf2, ELComputed = T)。我从SpatialLines 制作的对象没有第一个Lines 的第二个Lineidentical(read_sldf, read_sldf2); [1] TRUEreadshpnw() 显然只读了第一个 LineLines。如果给Detailed=Treadshpnw() 不会读取第二个Line
  • 感谢@cuttlefish44 的解释。我想不可能捕获物理网络(保持完整拓扑)并在必要时复制属性? readshpnw(sldf, Detailed = T) 返回Error in readshpnw(sldf, Detailed = T) : If a detailed graph is to be built, the properties of its attributes has to be specified
  • 据我所知,没有ea.propDetailed = T 就无法工作。 read_sldf_d = readshpnw(sldf, ELComputed = T, Detailed = T, ea.prop = rep(0, 5)) 似乎至少有关于每个第一个 Line 的所有坐标信息。

标签: r igraph


【解决方案1】:

只需更改 readshpnw 中的这些选项:

# create sldf object
require(sp); require(igraph); require(shp2graph)
d = data.frame(x = c(0,80,100,0,-20,-8,0,3,-10,-5,80,75),
               y = c(0,-10,5,0,14,33,0,-4,-10,-12,-10,5),
               grp = c(1,1,1,2,2,2,3,3,3,3,4,4))

sl = SpatialLines(list(
    Lines(list(Line(d[d$grp == 1,1:2]),
               Line(d[d$grp == 4,1:2])), ID=1),
    Lines(Line(d[d$grp == 2,1:2]), ID=2),
    Lines(Line(d[d$grp == 3,1:2]), ID=3))
)

sldf = SpatialLinesDataFrame(sl, iris[1:3,])
plot(sldf)

nodes = readshpnw(sldf, ELComputed = TRUE, Detailed = TRUE, ea.prop = names(sldf))
g = nel2igraph(nodes[[2]], nodes[[3]])
plot(g)

【讨论】:

  • 这看起来很合理。我什至不记得这个项目现在是关于什么的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 2011-08-19
相关资源
最近更新 更多