如何在R中的igraph中绘制网络度到节点大小,特征向量到x轴以及属性到y轴 [英] How to plot a network degree to node size and eigenvector to x axis and an attribute to y axis in igraph in R

查看:182
本文介绍了如何在R中的igraph中绘制网络度到节点大小,特征向量到x轴以及属性到y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将网络节点的大小设置为度中心,将x轴的分布设置为特征向量中心,而我将属性分配到y轴.我有以下代码:

I would like to set my network nodes size to degree centrality and the distribution on the x axis to eigenvector centrality while I distribute an attribute on the y axis. I have the following code:

    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()

除最后一个代码块外,所有内容均运行.我该怎么办?

Everything runs except the last code block. What do I need to do?

这是使用'dput(G1)'的网络的可打印示例和图形的打印:

here is a printable sample of the network using 'dput(G1)' and a print of the graph:

structure(list(22,TRUE,c(0,1,3,5,4,4,4,3,9,11,3,13,15、16、14、18、20、20),c(1、2、4、4、6、7、8、10、12、13、1413、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、1110、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、14、15、15、16、16、17),list(c(1,0,1),structure(list(),.Names = character(0)),列表(名称= c("P702","P617","P616","P619","P701","P212","P608","P625","P807","P623","P101","P613","P801","P606","P603","P602","P615","P252","P251","P238","P604","P624"),属性= c(7L,6L,6L,7L,7L,2L,6L,6L,8L,6L,1L,Pro = c(5,-4,6L,8L,6L,6L,6L,6L,2L,2L,2L,6L,6L)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"))))),class ="igraph")IGRAPH 0856078 DN-22 17-+ attr:名称(v/c),属性(v/n),Pro(v/n),类型(e/c)+来自0856078的边(顶点名称):[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

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"))), ), class = "igraph") 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

谢谢.就个人而言,我更喜欢细读表版本,我是r的新手,不确定如何附加它,但是在这里:

Thanks. personally, I prefer perusing the table version, I am new to r and unsure how to attach it, but here:

Vertex.1 Vertex.2类型

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

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

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

和顶点样本数据:

Vertex Property Pro

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

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

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

P604 6 2.0
P624 6 1.0

推荐答案

对于那些想要使用它的人来说,在文章结尾处是一个易于执行的数据版本.

For those who want to work with this, an easier to execute version of the data is at the end of the post.

我将假设您要布局节点的大小和位置.
顶点大小=中心度
x轴位置=特征值中心点
y轴位置= Pro(根据您的顶点属性)

I am going to assume that you want to layout the size and position of the nodes.
vertex size = degree centrality
x-axis position = eigenvalue centrality
y-axis position = Pro (from your vertex properties)

顶点大小相对简单.绘图时只需指定vertex.size参数即可.但是,顶点的度数范围为1到4,大多数为1.该值太小而不能直接用作顶点大小,因此您将需要首先缩放值.我选了一些看起来不错的东西.

The vertex size is relatively simple. Just specify the vertex.size parameter when plotting. However, the degrees of your vertices range from 1 to 4, mostly 1's. That is too small to use directly as the vertex size, so you will want to scale the values first. I picked something to look nice.

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

要放置节点,您需要指定布局.这就是一个简单的n x 2矩阵,其中x-y位置是您要绘制顶点的位置.同样,进行一些扩展可能会有所帮助.

To position the nodes, you need to specify a layout. That is simply a n x 2 matrix with the x-y position where you want the vertices plotted. Again, a bit of scaling may be helpful.

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

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

However, I suspect that you will want something that you did not specify and is just slightly harder to get - labeled axes so that you can read the x-y coordinates off the graph. This is a bit more work because the default igraph plotting rescales the position of the graph so that both axes run between +1 and -1. To keep the original scale, use rescale=FALSE. You have to specify the x-y limits and add the axes yourself. That also requires adjusting the vertex sizes.

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)

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轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆