R语言的igraph画社交关系图示例

    xiaoxiao2024-03-11  129

    R语言中的igraph可以很方便地画出社交关系图。下面是几个示例。

    1.最简单的社交关系图

    library(igraph)

    dolphin <- read.csv('dolphins.csv',head=T,fileEncoding='UTF-8',stringsAsFactors=F)

    g <- graph.data.frame(dolphin)

    jpeg(filename='dolphins.jpg',width=800,height=800,units='px')

    plot(g,

    vertex.size=5,

    layout=layout.fruchterman.reingold,

    vertex.shape='circle',

    vertex.label.cex=1.0,

    vertex.label.color='black',

    edge.arrow.size=0) #连线的箭头的大小为0,即无箭头

    dev.off()

    画出的图,如下:

    2.关系图中某人或某几个人的关系图

    某个人(这里是海豚)的关系图(节点4):

    jpeg(filename='dolphins_sub.jpg',width=800,height=800,units='px')

    gn<-graph.neighborhood(g, order=1)

    plot(gn[[1]],

    layout=layout.fruchterman.reingold)

    dev.off()

    某个人的两层关系图(节点6):

    gn<-graph.neighborhood(g, order=2)

    plot(gn[[2]], layout=layout.fruchterman.reingold)

    dev.off()

    某两个人的关系图:

    jpeg(filename='dolphins_sub3.jpg',width=800,height=800,units='px')

    gn<-graph.neighborhood(g, order=1)

    plot(gn[[1]]+gn[[2]], layout=layout.fruchterman.reingold)

    dev.off()

    3.根据联系人的多少决定节点的大小和色彩,连线设成弧线

    source("http://michael.hahsler.net/SMU/ScientificCompR/code/map.R")

    E(g)$curved <- 0.2 #将连线设成弧线,数值越大弧线越弯

    jpeg(filename='dolphins_curve1.jpg',width=800,height=800,units='px')

    layout=layout.fruchterman.reingold

    plot(g, layout=layout, vertex.size=map(degree(g),c(1,20)), vertex.color=map(degree(g),c(1,20)))

    dev.off()

    4.给社交关系图划分社区,不同的社区用不同的颜色表示

    cl <- optimal.community(g)

    E(g)$curved <- 0

    jpeg(filename='dolphins_commu2.jpg',width=800,height=800,units='px')

    layout=layout.fruchterman.reingold

    plot(g, layout=layout, vertex.size=5, vertex.color= rainbow(10, .8, .8, alpha=.8)[cl$membership+1L],)

    dev.off()

    5.设定社区的数目

    sg1 <- cluster_spinglass(g, spins=3, gamma=1.0) #spins是社区的数目

    jpeg(filename='dolphins_commu9.jpg',width=800,height=800,units='px')

    layout=layout.fruchterman.reingold

    plot(g, layout=layout, vertex.size=5, vertex.color= rainbow(10, .8, .8, alpha=.8)[sg1$membership],)

    dev.off()

    6.画出某一社区

    画出示例5的社区中,membership为1的社区。

    sg1 <- cluster_spinglass(g, spins=3, gamma=1.0)

    jpeg(filename='dolphins_subcommu.jpg',width=800,height=800,units='px')

    layout=layout.fruchterman.reingold

    subg <- induced.subgraph(g, which(membership(sg1)==1))

    plot(subg, layout=layout, vertex.size=5, vertex.color= 1,)

    dev.off()

    7.不同布局方式下社区的显示

    以上的布局为layout=layout.fruchterman.reingold

    如果是其他布局,社区的显示会有变化。

    sg1 <- cluster_spinglass(g, spins=3, gamma=1.0)

    jpeg(filename='dolphins_commu10.jpg',width=800,height=800,units='px')

    layout=layout.circle

    plot(g, layout=layout, vertex.size=5, vertex.color= rainbow(10, .8, .8, alpha=.8)[sg1$membership],)

    dev.off()

    sg1 <- cluster_spinglass(g, spins=3, gamma=1.0)

    jpeg(filename='dolphins_commu11.jpg',width=800,height=800,units='px')

    layout=layout.sphere

    plot(g, layout=layout, vertex.size=5, vertex.color= rainbow(10, .8, .8, alpha=.8)[sg1$membership],)

    dev.off()

    以上的示例仅为抛砖引玉。

    有关igraph的demo可以看demo(package="igraph")。

    在R中键入demo(package="igraph"),它会给出子项目:

    centrality Classic and other vertex centrality indices

    cohesive Cohesive blocking, the Moody & White method

    community Community structure detection

    crashR A crash-course into R

    hrg Hierarchical random graphs

    smallworld Small-world networks

    然后键入子项目看demo。如demo(package="igraph", community),它会给出community的示例。

    原文发布时间为:2015-08-31

    本文来自云栖社区合作伙伴“大数据文摘”,了解相关信息可以关注“BigDataDigest”微信公众号

    最新回复(0)