【发布时间】:2018-02-18 18:02:04
【问题描述】:
我正在尝试绘制一个随时间变化的网络。网络从一定数量的节点和边开始,每个时间步都会删除一些节点和边。
我希望能够绘制网络,以便每个节点都位于相同的位置。但是,当我尝试这个时。有时即使彼此的关系相同,节点也会在图框中移动位置。
我正在将网络更改为 gif,因此即使是很小的更改也很烦人。我认为当大部分节点被删除但我不确定时可能会发生变化。
下面的代码使用 ER 图说明了这一点。
library(igraph); library(dplyr)
#generate random graph
set.seed(500)
RandomGraph <- sample_gnm(1000, 2500)
#name nodes
V(RandomGraph)$name <- paste0("Node", 1:1000)
#Get the coordinates of the Nodes
Coords <- layout_with_fr(RandomGraph) %>%
as_tibble %>%
bind_cols(data_frame(names = names(V(RandomGraph))))
#Delete random vertices
deletevertex <-sample( V(RandomGraph)$name, 400)
RandomGraph2 <-delete.vertices(RandomGraph, deletevertex)
#get the coordinates of the remaining Nodes
NetCoords <- data_frame(names = names(V(RandomGraph2))) %>%
left_join(Coords, by= "names")
#plot both graphs
RandomGraph%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(Coords[,1:2]))
RandomGraph2%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(NetCoords[,2:3]))
#They nodes have the same relationship to each other but are not laid out in the same position in the frame
如您所见,图已将节点放置在相对于彼此但不相对于框架的相同位置。
如何固定绘图位置。
【问题讨论】: