r - 如何在 igraph 中绘制节点大小的网络度、x 轴的特征向量以及 y 轴的属性?

标签 r igraph

我想将网络节点大小设置为度中心性,将 x 轴上的分布设置为特征向量中心性,同时将属性分布在 y 轴上。我有以下代码:

u <- layout_with_dh(G1)

Aff <- "dashed"
Inf <- "solid"
Trig <- "dotted"
edge.lty = E(G1)$lty <- E(G_Network)$Type

start <- ends(G1, es = E(G1), names = F) [, 1]
edge.col <- V(G1)$color[start]

el <- c("Magenta", "olive drab", "spring green", "coral", "golden rod", "corn flower blue", "cyan", "Brown")
V(G1)$color <- el[V(G1)$Property]

EG <- eigen_centrality(G1, directed = T, scale = T, weights = NULL)


plot(EG, log = "xy", x = 0:max(EG), y = V(G1)$Pro, pch = 19, cex = 1, edge.width = 2, edge.arrow.size = .9, vertex.label.cex = 1, vertex.label.color = "black", vertex.label.font = 2, edge.curved = .5, edge.lty = E(G1), vertex.size = 3*igraph::degree(G1, mode = "out"), edge.color = edge.col, rescale = F, layout = u*1.3)
plot()

除了最后一个代码块之外,一切都会运行。我需要做什么?

这是使用“dput(G1)”的网络的可打印示例和图表的打印:

G1<- structure(list(22, TRUE, c(0, 1, 3, 5, 4, 4, 3, 9, 11, 3, 13, 
15, 16, 14, 18, 20, 20), c(1, 2, 4, 4, 6, 7, 8, 10, 12, 13, 14, 
13, 17, 16, 19, 16, 21), c(0, 1, 2, 6, 9, 4, 5, 3, 7, 8, 10, 
13, 11, 12, 14, 15, 16), c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 
10, 13, 15, 12, 14, 16), c(0, 1, 2, 2, 5, 7, 8, 8, 8, 8, 9, 9, 
10, 10, 11, 12, 13, 14, 14, 15, 15, 17, 17), c(0, 0, 1, 2, 2, 
4, 4, 5, 6, 7, 7, 8, 8, 9, 11, 12, 12, 14, 15, 15, 16, 16, 17
), list(c(1, 0, 1), structure(list(), .Names = character(0)), 
    list(name = c("P702", "P617", "P616", "P619", "P701", "P212", 
    "P608", "P625", "P807", "P623", "P101", "P613", "P801", "P606", 
    "P603", "P602", "P615", "P252", "P251", "P238", "P604", "P624"
    ), Property = c(7L, 6L, 6L, 7L, 7L, 2L, 6L, 6L, 8L, 6L, 1L, 
    6L, 8L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 6L, 6L), Pro = c(5, -4, 
    7, -6, 6, 2, 3, -5, -4, 2.5, 1.6, 6, 3, 1, -2, -5, 4.5, 2, 
    -3, 2, 2, 1)), list(Type = c("Trig", "Aff", "Inf", "Inf", 
    "Aff", "Aff", "Trig", "Inf", "Inf", "Inf", "Aff", "Aff", 
    "Inf", "Inf", "Aff", "Inf", "Inf"))), <environment>), class = "igraph")

## details
IGRAPH 0856078 DN-- 22 17 -- 
+ attr: name (v/c), Property (v/n), Pro (v/n), Type (e/c)
+ edges from 0856078 (vertex names):
 [1] P702->P617 P617->P616 P619->P701 P212->P701 P701->P608 P701->P625 P619->P807 P623->P101 P613->P801 P619->P606 P606->P603 P602->P606
[13] P615->P252 P603->P615 P251->P238 P604->P615 P604->P624

数据的表格版本:

Vertex.1   Vertex.2 Type

P702    P617    Trig        
P617    P616    Aff     
P619    P701    Inf     
P212    P701    Inf     
P701    P608    Aff     
P701    P625    Aff     
P619    P807    Trig        
P623    P101    Inf     
P613    P801    Inf     
P619    P606    Inf

P606    P603    Aff     
P602    P606    Aff     
P615    P252    Inf     
P603    P615    Inf     
P251    P238    Aff     
P604    P615    Inf     
P604    P624    Inf

和顶点样本数据:

Vertex  Property  Pro

P702    7   5.0     
P617    6   -4.0        
P616    6   7.0     
P619    7   -6.0        
P701    7   6.0     
P212    2   2.0     
P608    6   3.0     
P625    6   -5.0        
P807    8   -4.0        
P623    6   2.5

P101    1   1.6     
P613    6   6.0     
P801    8   3.0     
P606    6   1.0     
P603    6   -2.0        
P602    6   -5.0        
P615    6   4.5     
P252    2   2.0     
P251    2   -3.0        
P238    2   2.0

P604    6   2.0     
P624    6   1.0

最佳答案

对于那些想要使用此方法的人,文章末尾有一个更易于执行的数据版本。

我假设您想要布局节点的大小和位置。
顶点大小=度中心性
x 轴位置 = 特征值中心性
y 轴位置 = Pro(来自顶点属性)

顶点大小比较简单。只需在绘图时指定 vertex.size 参数即可。但是,顶点的度数范围为 1 到 4,大部分为 1。该值太小,无法直接用作顶点大小,因此您需要首先缩放这些值。我选了一些看起来不错的东西。

VS = 6 + 5*degree(g)
plot(g, vertex.size=VS)

graph with vertex size specified

要定位节点,您需要指定布局。这只是一个 n x 2 矩阵,其中包含要绘制顶点的 x-y 位置。同样,进行一些缩放可能会有所帮助。

x = round(10*eigen_centrality(g)$vector, 2)
y = vertex_attr(g, "Pro")
LO = cbind(x,y)
plot(g, vertex.size=VS, layout=LO)

Size and position specified

但是,我怀疑您会想要一些您未指定的东西,并且稍微难以获得 - 标记轴,以便您可以从图表中读取 x-y 坐标。这需要更多的工作,因为默认的 igraph 绘图会重新调整图形的位置,以便两个轴都在 +1 和 -1 之间运行。要保持原始比例,请使用rescale=FALSE。您必须指定 x-y 限制并自行添加轴。这还需要调整顶点大小。

plot(g, vertex.size=7*VS, layout=LO, rescale=F, 
    xlim=range(x), ylim=range(y))
axis(side=1, pos=min(y)-1)
axis(side=2)

Final version of graph

数据和图表创建

EL = read.table(text="Vertex.1 Vertex.2 Type
P702 P617 Trig
P617 P616 Aff
P619 P701 Inf
P212 P701 Inf
P701 P608 Aff
P701 P625 Aff
P619 P807 Trig
P623 P101 Inf
P613 P801 Inf
P619 P606 Inf
P606 P603 Aff
P602 P606 Aff
P615 P252 Inf
P603 P615 Inf
P251 P238 Aff
P604 P615 Inf
P604 P624 Inf",
header=T)

VERT = read.table(text="Vertex Property Pro
P702 7 5.0
P617 6 -4.0
P616 6 7.0
P619 7 -6.0
P701 7 6.0
P212 2 2.0
P608 6 3.0
P625 6 -5.0
P807 8 -4.0
P623 6 2.5
P101 1 1.6
P613 6 6.0
P801 8 3.0
P606 6 1.0
P603 6 -2.0
P602 6 -5.0
P615 6 4.5
P252 2 2.0
P251 2 -3.0
P238 2 2.0
P604 6 2.0
P624 6 1.0",
header=T)

g = graph_from_data_frame(EL, directed=FALSE, vertices=VERT)

关于r - 如何在 igraph 中绘制节点大小的网络度、x 轴的特征向量以及 y 轴的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59391010/

相关文章:

r - 使用 reshape2 转置表

r - 表达式()中的换行符?

r - twitter api 的 httr::oauth1.0_token 错误

r - 使用 InfoMap 算法进行社区检测,产生一个庞大的模块

r - 彩色图同构 : 1(red)->2(blue) vs 1(blue)->2(red)

r - 数据抓取;使用 rvest 从表中提取链接

r - 运行长度的累积和。这个循环可以矢量化吗?

R igraph 图密度、直径、偏心率和最短路径

R igraph 将平行边转换为权重属性

r - 在 R 中,如何在节点之间随机生成边?