在相同位置绘制顶点 [英] Plot vertices at the same position
本文介绍了在相同位置绘制顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法可以在同一位置绘制2个图的共享节点?例如,两个图
Is there a method to plot the shared nodes of 2 graphs at the same position? E.g., two graphs
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个节点完全相同.如何绘制具有相同位置的相同节点的图,以便于比较差异?
There are 3 nodes are exactly the same for g1 and g2. How can I plot them with the same nodes having same position so that its easy to compare the difference?
par(mfrow=c(1,2))
plot(g1, vertex.label=V(g1)$name)
plot(g2, vertex.label=V(g2)$name)
推荐答案
使用注释中链接到的问题的代码,您可以从一个图形中提取位置,并在另一个图形上使用它们.
Using the code from the question linked to in the comments you can take the positions from one graph and use them on another.
# 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)
否则为一组节点和边着色
Or else colour one set of nodes and edges
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)
这篇关于在相同位置绘制顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文