问题
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]
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)
回答1:
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]))
Plot side by side
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)

来源:https://stackoverflow.com/questions/24329309/plot-vertices-at-the-same-position