R:nel2igraph和PN.MEGALION-igraph未正确生成
我遇到软件包R:nel2igraph和PN.MEGALION-igraph未正确生成,r,igraph,shapefile,R,Igraph,Shapefile,我遇到软件包shp2graph的问题。我想使用功能PN.mergation,该功能运行良好(见下文)。之后,我想创建一个igraph对象。在这里,代码无法做到这一点。 我可以使用每个未合并的shp2graph对象创建igraph对象 这里是我的示例代码,它主要是来自包描述的复制粘贴shp2graph: library(igraph) library(shp2graph) data(ORN) rtNEL<-readshpnw(ORN.nt, ELComputed=TRUE) res.sl&
shp2graph
的问题。我想使用功能PN.mergation
,该功能运行良好(见下文)。之后,我想创建一个igraph对象。在这里,代码无法做到这一点。
我可以使用每个未合并的shp2graph对象创建igraph对象
这里是我的示例代码,它主要是来自包描述的复制粘贴shp2graph
:
library(igraph)
library(shp2graph)
data(ORN)
rtNEL<-readshpnw(ORN.nt, ELComputed=TRUE)
res.sl<-SL.extraction(rtNEL[[2]],rtNEL[[3]])
res.me<-ME.simplification(res.sl[[1]],res.sl[[2]],DegreeL=res.sl[[4]])
res.pn<-PN.amalgamation(res.me[[1]],res.me[[2]],DegreeL=res.me[[4]])
ptcoords<-Nodes.coordinates(res.pn[[1]])
plot(ORN.nt)
points(ptcoords, col="green")
plot(ORN.nt)
points(Nodes.coordinates(rtNEL[[2]]), col="red")
# igraph created from amalgamation is wrong
test <- nel2igraph(nodelist= res.pn[[1]], edgelist=res.pn[[2]], Directed = TRUE)
plot(test,vertex.size=1,edge.width=1,edge.arrow.size=0,vertex.label=NA)
# res.me is one step before amalgamation
test <- nel2igraph(nodelist= res.me[[1]], edgelist=res.me[[2]], Directed = TRUE)
plot(test,vertex.size=1,edge.width=1,edge.arrow.size=0,vertex.label=NA)
库(igraph)
图书馆(shp2graph)
数据(ORN)
rtNEL我发现缺陷不知何故存在于与igraph
包的交互中。问题是由PN.mergalization
创建的节点标签不再连续;有些丢失了,因为我们删除了它们。但是,igraph
仍试图创建它们,并发出以下警告:
对于任何有同样问题的人来说,这里有一个解决办法,它可以重新标记标签
创建自己的nel2igraph
函数:
nel2igraph_corr <- function (nodelist, edgelist, weight = NULL, eadf = NULL, Directed = FALSE)
{
nodes <- nodelist[, 1]
Ne <- length(edgelist[, 1])
Nn <- length(nodes)
for (i in 1:Nn) {
kk <- nodelist[i,][[1]]
edgelist[which(edgelist[,c(2)]==kk),2] <- i
edgelist[which(edgelist[,c(3)]==kk),3] <- i
nodelist[i,][[1]] <- i
}
if (!is.null(weight)) {
if (length(weight) != Ne && is.numeric(weight))
stop("Please give right edge weight, which must be numeric and the same length as edges elment")
}
if (!is.null(eadf)) {
if (length(eadf[, 1]) != Ne)
stop("The eadf must be numeric and the same length as edges elment")
}
gr <- graph.edgelist(unique(edgelist[, c(2, 3)]), directed = T)
gr <- set.vertex.attribute(gr, "x", V(gr), Nodes.coordinates(nodelist)[,1])
gr <- set.vertex.attribute(gr, "y", V(gr), Nodes.coordinates(nodelist)[,
2])
gr.es <- E(gr)
if (!is.null(weight))
gr <- set.edge.attribute(gr, "weight", gr.es, weight)
if (!is.null(eadf)) {
eanms <- colnames(eadf)
n <- length(eanms)
for (i in 1:n) gr <- set.edge.attribute(gr, eanms[i],
gr.es, eadf[, i])
}
gr
}
nel2igraph\u corr