如何用R更清晰地绘制社交网络图?

时间:2018-05-28 01:53:08

标签: r graph nodes igraph network-analysis

我现在正在使用lesmis.gml进行网络分析作业。 我无法调整图节点的距离:节点超过70个,节点距离太近。 图是变量g和g2。 graph looks weird like this.(image) 这是我使用R的代码 我尝试使用Gephi,但我的笔记本电脑运行得不好。它关闭了。

install.packages('igraph')
install.packages('statnet')
library('igraph')
library('statnet')
g<-read.graph("lesmis.gml", format=c("gml"))
g
graph.density(g)
igraph::degree(g,mode="out")
plot(g)
vcount(g)
centralization.degree(g)
V(g)$size<-igraph::degree(g)*5
plot(g)
clo<-igraph::closeness(g)
clo
clo.score<-round((clo-min(clo))*length(clo)/max(clo))+1
clo.colors<-rev(heat.colors(max(clo.score)))
V(g)$color<-clo.colors[clo.score]
plot(g)
btw<-igraph::betweenness(g)
btw
btw.score<-round(btw)+1
btw.score
btw.colors<-rev(heat.colors(max(btw.score)))
V(g)$color<-btw.colors[btw.score]
plot(g)
clusters(g)
clusters(g)$csize
cliques(g)
sapply(cliques(g), length)
largest_cliques(g)
cliques(g)
sapply(cliques(g),length)
a<-largest_cliques(g)
a
clique1<-a[[1]]
g2<-induced.subgraph(graph=g,vids=clique1)
plot(g2)
vcol<-rep("grey80",vcount(g))
vcol[unlist(largest_cliques(g))]<-"gold"
plot(as.undirected(g),vertex.lavel=V(g)$name, vertex.color=vcol)
windows()

1 个答案:

答案 0 :(得分:1)

我有两个建议。在介绍它们之前,我将设置基础知识,以便我所做的(大多数)是可重复的。这只是代码中的简化版本,可以更改顶点大小。

library(igraph)
g<-read.graph("temp/lesmis.gml", format=c("gml"))
V(g)$size<-igraph::degree(g)/2

btw<-igraph::betweenness(g)
btw.score<-round(btw)+1
btw.colors<-rev(heat.colors(max(btw.score)))
V(g)$color<-btw.colors[btw.score]
  1. 我认为这就是@nhl所暗示的。 igraph中有相当多的布局功能。试试看,他们看看看起来不错。我有点喜欢大图形布局。

    set.seed(1234) LO_LGL = layout_with_lgl(g) plot(as.undirected(g),layout = LO_LGL,margin = c(-0.25,-0.25))

  2. LesMis - just LGL

    1. 一旦得到非常接近的内容,您可以尝试使用tkplot,这将允许您选择节点并移动它们以使图表更具可读性。
    2. tkplot(as.undirected(g), layout=LO_LGL)

      我使用之前的布局作为起始位置并手动调整顶点以使图形更清晰。它并不完美,但你可以看到一些社区。

      LesMis - tkplot

相关问题