在同一位置绘制顶点

时间:2014-06-20 14:12:53

标签: r plot position igraph

是否有方法在同一位置绘制2个图的共享节点?例如,两个图表

g1 = graph.ring(5) 
V(g1)$name=c('node1','node2','node3','node4','node5')
g1 = g1 - V(g1)[1]


g2 = graph.ring(5)
V(g2)$name=c('node1','node2','node3','node4','node5')
g2 = g2 - V(g2)[2]

g1和g2有3个节点完全相同。如何使用具有相同位置的相同节点绘制它们以便于比较差异?

par(mfrow=c(1,2))
plot(g1, vertex.label=V(g1)$name)
plot(g2, vertex.label=V(g2)$name)

1 个答案:

答案 0 :(得分:4)

使用评论中链接的问题代码,您可以从一个图表中获取位置,并在另一个图表中使用它们。

# Graphs - tweaked the node names
g1 = graph.ring(5) 
V(g1)$name=letters[1:5]
g1 = g1 - V(g1)[1]

g2 = graph.ring(5)
V(g2)$name=letters[2:6]
g2 = g2 - V(g2)[2]


# graph layouts
# g1
set.seed(1)
layg1 <- layout.fruchterman.reingold(g1)

# g2
set.seed(2)
layg2 <- layout.fruchterman.reingold(g2)
# overwrite coords for shared nodes
layg2[which(V(g2)$name %in% V(g1)$name), ] <- 
                                      layg1[which(V(g1)$name %in% V(g2)$name),]

xlim <- range(c(layg1[,1], layg2[,1]))
ylim <- range(c(layg1[,2], layg2[,2]))

并排绘图

par(mfrow=c(1,2))
plot(g1 , vertex.size=50, layout=layg1, xlim=xlim, ylim=ylim, rescale=FALSE)
plot(g2 , vertex.size=50, layout=layg2, xlim=xlim, ylim=ylim, rescale=FALSE)

enter image description here

或者为一组节点和边缘着色

V(g2)$color <- "red"
E(g2)$color <- "red"

plot(g1 , vertex.size=50, layout=layg1, xlim=xlim, ylim=ylim, rescale=FALSE)
plot(g2 , vertex.size=30, layout=layg2, xlim=xlim, ylim=ylim, rescale=FALSE, add=T)

enter image description here