R包igraph探究
前段时候由于项目的原因,需要画图,然后开始接触R语言的igraph包,网上零零散散的搜罗了不少的信息,放在这边交流分享的同时也给自己留个备份吧~
1.首先是读取文件,基本选用的都是csv文件
edge1<-read.csv("D:/9th_smj/onetimecut.csv",header=F) vertex3<-read.csv("D:/9th_smj/vertex.csv",header=F)
2.设置变量的格式
edge1[,1]=as.character(edge1[,1]) edge1[,2]=as.character(edge1[,2]) edge1[,3]=as.character(edge1[,3]) edge1[,4]=as.character(edge1[,4])
edge<-edge1[c(1,3)] vertex3[,1]=as.character(vertex3[,1]) vertex3[,2]=as.character(vertex3[,2])
3.图构建
people = data.frame(id = vertex3[,1], name = vertex3[,2]) g=graph.data.frame(d=edge,direct=T,vertices=people)
4.画图
png('D:/9th_smj/cuttoone.png',width=900,height=900) set.seed(20)#设定种子节点,同一种布局画出来的图就是可以重复,不然即使同一种布局,展现的时候由于位置的随机也会呈现出不一样的结果 plot(g, layout = layout.fruchterman.reingold, vertex.size = V(g)$size+2,vertex.color=V(g)$color,vertex.label=V(g)$label,vertex.label.cex=1,edge.color = grey(0.5), edge.arrow.mode = "-",edge.arrow.size=5) dev.off()
针对plot函数的一些参数,特别解释下:
4.1 layout设置图的布局方式
选项有:layout、layout.auto、layout.bipartite、layout.circle、layout.drl、layout.fruchterman.reingold、layout.fruchterman.reingold.grid、layout.graphopt、layout.grid、layout.grid.3d、layout.kamada.kawai、layout.lgl、layout.mds、layout.merge、layout.norm、layout.random、layout.reingold.tilford、layout.sphere、layout.spring、layout.star、layout.sugiyama、layout.svd
4.2 vertex.size设置节点的大小:不同节点不同大小
de<-read.csv("c:/degree-info.csv",header=F) V(g)$deg<-de[,2] V(g)$size=2 V(g)[deg>=1]$size=4 V(g)[deg>=2]$size=6 V(g)[deg>=3]$size=8 V(g)[deg>=4]$size=10 V(g)[deg>=5]$size=12 V(g)[deg>=6]$size=14
4.3 vertex.color设置节点的颜色:不同标记有不用的颜色
color<-read.csv("c:/color.csv",header=F) col<-c("red","skyblue") V(g)$color=col[color[,1]]
4.4 vertex.label设置节点的标记
V(g)$label=V(g)$name vertex.label=V(g)$label
4.5 vertex.label.cex设置节点标记的字体大小
4.6 edge.color设置边的颜色:不同的边有不一样的颜色
E(g)$color="grey" for(i in 1:length(pa3[,1])){ E(g,path=pa3[i,])$color="red" } edge.color=E(g)$color
4.7 edge.arrow.mode设置边的连接方式
4.8 edge.arrow.size设置箭头的大小
4.9 设置边的宽度
E(g)$width=1
5.聚类分析
感觉igraph包所提供的聚类算法还是很多的,将几种常用的列出,随后有时间的话再附上算法思想及参考文献
5.1 边的中介度聚类
system.time(ec <- edge.betweenness.community(g)) print(modularity(ec)) plot(ec, g,vertex.size=5,vertex.label=NA)
5.2 随机游走
system.time(wc <- walktrap.community(g)) print(modularity(wc)) #membership(wc) plot(wc , g,vertex.size=5,vertex.label=NA)
5.3 特征值(个人理解觉得类似谱聚类)
system.time(lec <-leading.eigenvector.community(g)) print(modularity(lec)) plot(lec,g,vertex.size=5,vertex.label=NA)
5.4 贪心策略?
system.time(fc <- fastgreedy.community(g)) print(modularity(fc)) plot(fc, g,vertex.size=5,vertex.label=NA)
5.5 多层次聚类
system.time(mc <- multilevel.community(g, weights=NA)) print(modularity(mc)) plot(mc, g,vertex.size=5,vertex.label=NA)
5.6 标签传播
system.time(lc <- label.propagation.community(g)) print(modularity(lc)) plot(lc , g,vertex.size=5,vertex.label=NA)
6.文件输出
zz<-file("d:/test.txt","w") cat(x,file=zz,sep="\n") close(zz)
7.查看变量数据类型和长度
mode(x) length(x)