简体   繁体   中英

fixing nodes when plotting networks

mynet is a network object with the 93 vertices and three vertex attributes: sex , indegree , and outdegree . Another network object, simnet , is simulated version of the network. The nodes and degree distributions are the same, but some edges have been rewired.

I plot them side by side...

par(mfrow=c(1,2))
plot(mynet, vertex.col="sex", main="mynet")
plot(simnet, vertex.col="sex", main="simnet")

...and get the following result:

在此处输入图片说明

This would be much more useful if I could fix the node location in both plots, as it would make the differences in edges very clear. Is there a way to do this with the base plot() function? If not, what is the simplest way to do this without manually entering coordinates for each node?

There is a way to do this by setting the layout in advance of plotting and using the same layout for both plots. We can do this using the names of the nodes since these are the same nodes between each graph. The approach is a little hacky but seems to work. Example code below:

library(igraph)

# Make some fake networks
set.seed(42)
df1 <- data.frame(e1 = sample(1:5, 10, replace = T),
                  e1 = sample(1:5, 10, replace = T))

df2 <- data.frame(e1 = sample(1:5, 10, replace = T),
                  e1 = sample(1:5, 10, replace = T))

# the original
g1 <- graph_from_data_frame(df1, directed = F)
# the 'simulations'
g2 <- graph_from_data_frame(df2, directed = F)

# set up the plot
par(mfrow=c(1,2))
# we set the layout
lo <- layout_with_kk(g1)
# this is a matrix of positions. Positions
# refer to the order of the nodes
head(lo)
#>             [,1]        [,2]
#> [1,] -0.03760207  0.08115827
#> [2,]  1.06606602  0.35564140
#> [3,] -1.09026110  0.28291157
#> [4,] -0.90060771 -0.72591181
#> [5,]  0.67151585 -1.82471026
V(g1)
#> + 5/5 vertices, named, from 418e4e6:
#> [1] 5 2 4 3 1

# If the layout has names for the rows then we can
# use those names to fiddle with the order
row.names(lo) <- names(V(g1))

# plot with layout
plot(g1, layout = lo)
# plot with layout but reorder the layout to match the order
# in which nodes appear in g2
plot(g2, layout = lo[names(V(g2)), ])

Created on 2018-11-15 by the reprex package (v0.2.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM