4

我一直在尝试使用ggnet2. 为此,我使用以下代码:

library(igraph)
lapply(c("sna", "intergraph", "GGally", "igraph", "network"), require, character.only=T)
data <- read.table('CA-CondMat.txt',sep="\t",header=TRUE)
g = graph.data.frame(data, directed = TRUE)
N = vcount(g)
E = ecount(g)
perc = 0.1
d.g = degree(g,mode='all')/N
new_nodes = sample.int(N,ceiling(perc*N),replace=FALSE,prob =d.g)
new_g = subgraph(g,new_nodes)
dg = degree(g,mode='all')
prob = dg/sum(dg)
png('example_plot2.png')
ggnet2(new_g, size = "degree", node.color = "steelblue", size.cut = 4,
                                          edge.size = 1, edge.color="grey" )
dev.off()

我得到一个完全蓝色的图表。

我正在使用包igraph

我想要绘制的是一个基于节点颜色的图表,如下所示: 在此处输入图像描述

文件链接:
https ://snap.stanford.edu/data/ca-CondMat.html

编辑:

添加了完整示例

4

1 回答 1

7

我喜欢挑战,图表总是很有趣。我认为这就是您想要的(我修改了我的原始文件以使用您稍后提供的文件,因为我正在处理它):

在我的代码clr-degree中是度数的一半,因为这个文件只有对称链接,没有黑色和绿色节点看起来很无聊。

我还为所有调用添加了库前缀,这样我就可以从哪个网络库(igraph、网络等)中看到正在使用的内容。这些库中有很多重叠和相互依赖。

请注意,此代码应映射clr-degree0-1to black、 degree 2to red、 degree 3togreen>=4to red

library(ggplot2)
library(igraph)
library(GGally)

# the following libraries will be required too - used internally
lapply(c("sna", "scales","intergraph", "network"),require, character.only=T)

set.seed(1234)

# data from  https://snap.stanford.edu/data/ca-CondMat.html
data <- read.table('CA-CondMat.txt',sep="")

g = igraph::graph.data.frame(data, directed = TRUE)
N = vcount(g)
E = ecount(g)
d.g = igraph::degree(g,mode='all')/N

# Use new smaller subgraph
perc = 0.05
new_nodes = sample.int(N,ceiling(perc*N),replace=FALSE,prob =d.g)
new_g = igraph::subgraph(g,new_nodes)
dg = igraph::degree(new_g,mode='all')

dg <- dg/2  # for some reason there are only even degrees in this file - so we divide by 2

clrvek = pmax(0,pmin(dg,4))
clrnames = c("0"="lightgrey","1"="black", "2"="blue", "3"="green", "4"="red")

#png('example_plot2.png')
GGally::ggnet2(new_g, 
       color.legend="clr-degree",palette=clrnames,color=clrvek,
       size = "degree",
       edge.size = 1, edge.color="grey",
       legend.position = "bottom") + coord_equal()
#dev.off()

产量:

在此处输入图像描述

于 2016-05-23T20:32:38.487 回答