R索引矩阵以在绘图坐标中使用

问题描述 投票:0回答:2

我正在尝试在R中绘制时间社交网络。我的方法是为所有节点创建主图和布局。然后,我将基于一系列顶点id来对图形进行子集化。但是,当我这样做并布局图形时,我得到完全不同的节点位置。我想我要么错误地对布局矩阵进行子集化。我找不到我的问题所在,因为我做了一些较小的矩阵子集,一切似乎都运行正常。

我有一些示例代码和网络图中的问题图像。

library(igraph)

# make graph
g <- barabasi.game(25)




# make graph and set some aestetics 
set.seed(123)
l <- layout_nicely(g)
V(g)$size <- rescale(degree(g), c(5, 20))
V(g)$shape <- 'none'
V(g)$label.cex <- .75
V(g)$label.color <- 'black'

E(g)$arrow.size = .1

# plot graph

dev.off()

par(mfrow = c(1,2), 
    mar = c(1,1,5,1))


plot(g, layout = l, 
     main = 'Entire\ngraph')


# use index & induced subgraph

v_ids <- sample(1:25, 15, F)
sub_l <- l[v_ids, c(1,2)]
sub_g <- induced_subgraph(g, v_ids)


# plot second graph
plot(sub_g, layout = sub_l,
     main = 'Sub\ngraph') 

enter image description here

第二个图中的顶点应与第一个图中的顶点匹配。

r matrix plot igraph
2个回答
1
投票

不幸的是,您在生成图表后设置了随机种子,因此我们无法准确地重现您的结果。在图表生成之前,我将使用相同的代码,但使用set.seed。这使得结果看起来与您的不同,但是可以重现。

当我运行您的代码时,我看不到与您显示的完全相同的问题。

你的代码(移动了set.seed并添加了scales

library(igraph)
library(scales)            # for rescale function

# make graph
set.seed(123)
g <- barabasi.game(25)

# make graph and set some aestetics 
l <- layout_nicely(g)
V(g)$size <- rescale(degree(g), c(5, 20))
V(g)$shape <- 'none'
V(g)$label.cex <- .75
V(g)$label.color <- 'black'
E(g)$arrow.size = .1
## V(g)$names = 1:25

# plot graph
dev.off()

par(mfrow = c(1,2), 
    mar = c(1,1,5,1))

plot(g, layout = l, 
     main = 'Entire\ngraph')

# use index & induced subgraph
v_ids <- sort(sample(1:25, 15, F))
sub_l <- l[v_ids, c(1,2)]
sub_g <- induced_subgraph(g, v_ids)

# plot second graph
plot(sub_g, layout = sub_l,
     main = 'Sub\ngraph', vertex.label=V(sub_g)$names) 

Graph with positions preserved, but not labels

当我运行你的代码时,两个图都有相同位置的节点。这不是我在你的问题中的图表中看到的。我建议您只运行此代码,看看是否得到相同的结果(两个图中相同位置的节点)。我的版本中两个图表之间的唯一区别是节点标签。当您获取子图时,它会将节点重新编号为1到15,因此节点上的标签不一致。您可以通过在获取子图之前将节点标签存储在图形中来解决此问题。具体来说,在你的陈述V(g)$names = 1:25之后立即添加E(g)$arrow.size = .1。然后从set.seed(123)开始再次运行整个事情。这将保留原始编号作为节点标签。

Graph with node labels preserved

该图看起来略有不同,因为新的子图不占用所有空间,因此被拉伸以用尽空白空间。


-1
投票

可能的快速方法:绘制相同的图形,但颜色节点和顶点,你不需要的背景颜色。根据您的目的,它可以适合您。

© www.soinside.com 2019 - 2024. All rights reserved.