How to keep position of nodes in igraph + R

泄露秘密 提交于 2020-01-03 16:51:20

问题


If I have the following graph and I set the position coordinates of nodes with a vector:


library(igraph)
library(Cairo)

g9<- graph(c(0,1,0,2,0,3,1,4,1,2,3,4,3,5,4,5,5,2),n=6,dir=FALSE)
V(g9)$name<-c(1:6)
V(g9)$label<-V(g9)$name
coords <- c(0, 0, 1.00000000000000, 0,0.500000000000000, 0.866025403784439, 0.300000000000000, 0.200000000000000, 0.441421356237309, 0.341421356237310,0.248236190979496,0.393185165257814)
coords <- matrix(coords, 6,2,byrow=T)
plot(g9,layout=coords)

Now if I add the following code, I have to set the coordinate layout again:


vc<-c(0,1,1,2,2,3,3,1)
gp<-graph(vc,dir=FALSE);
listSub<-graph.get.subisomorphisms.vf2(g9,gp)

m<-matrix(c(unlist(listSub)),length(listSub),length(unique(vc)),byrow=T)
md<-m[!duplicated(m[,1]),]

g<-delete.vertices(g9,c(m[1,2]))

dev.new()

coord <- c(0, 0, 1.00000000000000, 0,0.500000000000000, 0.866025403784439, 0.441421356237309, 0.341421356237310,0.248236190979496,0.393185165257814)
coord <- matrix(coord, 5,2,byrow=T)
plot(g,layout=coord)

How do I make these layout positions the default -- so that I can make modifications on the graph's structure without losing the corresponding position for each name?



回答1:


You could keep the initial coord matrix and only extract the rows needed for the subgraph.

plot(g9, layout = coords[ V(g9)$name, ])
plot(g , layout = coords[  V(g)$name, ])


来源:https://stackoverflow.com/questions/10085046/how-to-keep-position-of-nodes-in-igraph-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!