library(igraph)
创建graph并绘图
#graph_from_literal()
#To create small graphs with a given structure probably the graph_from_literal function is easiest.
graph_from_literal()
## IGRAPH U--- 0 0 --
## + edges:
graph_from_literal(A-B)
## IGRAPH UN-- 2 1 --
## + attr: name (v/c)
## + edge (vertex names):
## [1] A--B
graph_from_literal( A-----B )
## IGRAPH UN-- 2 1 --
## + attr: name (v/c)
## + edge (vertex names):
## [1] A--B
graph_from_literal( A--B, C--D, E--F, G--H, I, J, K )
## IGRAPH UN-- 11 4 --
## + attr: name (v/c)
## + edges (vertex names):
## [1] A--B C--D E--F G--H
graph_from_literal( A +- B -- C )
## IGRAPH DN-- 3 1 --
## + attr: name (v/c)
## + edge (vertex names):
## [1] B->A
graph_from_literal( "this is" +- "a silly" -+ "graph here" )
## IGRAPH DN-- 3 2 --
## + attr: name (v/c)
## + edges (vertex names):
## [1] a silly->this is a silly->graph here
#make_graph(edges, ..., n = max(edges), isolates = NULL, directed = TRUE, dir = directed, simplify = TRUE)
# 可以使用最基本的make_graph函数,用向量作为参数来创建图形,之后用plot绘制出结果
g1 <- make_graph( c(1, 2, 2, 3, 3, 4, 4,5,5, 6),directed = T)
plot(g1,layout=layout.circle(g1))
#make_star()
# 也可以画出一些特殊结构的图形,例如下面的星形图
g2 <- make_star(10, mode = "in")
plot(g2,layout=layout.fruchterman.reingold(g2))
#graph_from_edgelist(el, directed = TRUE)
#graph_from_edgelist creates a graph from an edge list. Its argument is a two-column matrix, each row defines one edge.
el <- matrix( c("foo", "bar", "bar", "foobar"), nc = 2, byrow = TRUE)
g2<- graph_from_edgelist(el)
plot(g2,layout=layout.circle(g2))
#graph_from_data_frame(d, directed = TRUE, vertices = NULL)
#as_data_frame(x, what = c("edges", "vertices", "both"))
#This function creates an igraph graph from one or two data frames containing the (symbolic) edge list and edge/vertex attributes.
d = data.frame(p1 = c('a', 'b', 'c'),
p2 = c('b', 'c', 'a'),
weight = c(1, 2, 4))
g = graph.data.frame(d, directed = TRUE)
plot(g, edge.width = E(g)$weight)
顶点深度反应顶点中心度
连线粗细反应部门间的联系大小
t=read.csv("42部门投入产出表.csv",header=T,stringsAsFactors = F)[,-1]
X=sapply(t,sum)
#求直接消耗系数a
a=matrix(nrow = 42,ncol = 42)
for(i in 1:42){
for(j in 1:42){
a[i,j]=t[i,j]/X[j]
}
}
#转换成 中间投入-中间使用-直接消耗系数 数据框
d=data.frame(stringsAsFactors = F)
for(i in 1:42){
p1=rep(i,42)
p2=c(1:42)
weight=a[i,]
temp=data.frame(p1,p2,weight)
d=rbind(d,temp)
}
#筛选直接消耗系数大于0.1的行,并删掉中间投入与中间使用相同的行
d1=d[(d$weight>0.1)&(d$p1!=d$p2),]
#画图
g = graph.data.frame(d1, directed = T)
deg=degree(g,mode = "out")+1 #根据顶点中心度大小生成调节参数
weight=(d1$weight)^0.35*3
#方法一
set.seed("1234")#设置随机数起点。保证plot出一样的图
V(g)$color= rgb(0.6,0,0.2,deg/max(deg)) #根据顶点度大小设置颜色深浅
V(g)$frame.color = NA
V(g)$label.color='black'
V(g)$label.font=2
E(g)$width=weight #根据边的权重设置边粗细
E(g)$color='blue'
E(g)$arrow.size=0.3
plot(g)
#方法二
set.seed("1234")
plot(g, vertex.color= rgb(0.6,0,0.2,deg/max(deg)), vertex.frame.color = NA,
vertex.label.color='black', vertex.label.font=2, edge.width=weight,
edge.color='steelblue', edge.arrow.size=0.3)
#看看直接消耗系数取其他临界值的情况
fun=function(d,Weight){
d1=d[(d$weight>Weight)&(d$p1!=d$p2),]
g = graph.data.frame(d1, directed = T)
deg= degree(g,mode = "out")+3 #根据顶点度大小生成调节参数
weight=(d1$weight)^0.35*3
set.seed("1234")
plot(g, vertex.color= rgb(0.6,0,0.2,deg/max(deg)), vertex.frame.color = NA,
vertex.label.color='black', vertex.label.font=2, edge.width=weight,
edge.color='steelblue', edge.arrow.size=0.3)
}
fun(d,0.1)
fun(d,0.05)
fun(d,0.02)
中心度研究能够识别网络中的重要节点,节点的重要程度由网络的拓扑属性、结构特点及节点在网络中的具体位置决定。MikeGotta指出“中心度的概念,简单来说是识别网络中具有高度连接的活动者”。这个定义有些片面,而维基百科中也没有给出明确的定义,给出的是“在图论和网络分析中,对一个节点的多种中心度测量,这些测量主要是决定图中一个节点的相对重要性。”的一种解释。
之前的研究中并没有给出关于中心度的严格定义,本文认为它是关于节点重要性的度量指标。 这种重要性, 根据不同网络和结构特点及关系表现为节点的影响力、权威性( 重要思想、 知识或判断决策的源头)、 流行度、控制力(如传输、流量的控制能力),便利性(位置上的优势、易于访问)或某种特殊意义,也可以表现为节点的脆弱性和易受攻击性。不同的中心度算法含义不同,此处给出的是一个总结性质的定义。