使用 ggnet2 在 R 中按度数着色
Color by degree in R using ggnet2
我一直在尝试使用 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
。
我想要绘制的是一个图表,其中节点的颜色基于节点的度数,如下所示:
Link 到文件:
https://snap.stanford.edu/data/ca-CondMat.html
编辑:
已添加完整示例
我喜欢挑战,图表总是很有趣。我想这就是你想要的(我修改了我的原件以使用你稍后在我处理它时提供的文件):
在我的代码中 clr-degree
是度数的二分之一,因为这个文件只有对称链接,没有黑色和绿色节点看起来很无聊。
我还为所有调用添加了库前缀,这样我就可以看到正在使用哪些网络库(igraph、网络等)。这些库中有很多重叠和相互依赖关系。
请注意,此代码应将 clr-degree
映射到 0-1
到 black
、度数 2
到 red
、度数 3
到 green
,以及 >=4
到 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()
产量:
我一直在尝试使用 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
。
我想要绘制的是一个图表,其中节点的颜色基于节点的度数,如下所示:
Link 到文件:
https://snap.stanford.edu/data/ca-CondMat.html
编辑:
已添加完整示例
我喜欢挑战,图表总是很有趣。我想这就是你想要的(我修改了我的原件以使用你稍后在我处理它时提供的文件):
在我的代码中 clr-degree
是度数的二分之一,因为这个文件只有对称链接,没有黑色和绿色节点看起来很无聊。
我还为所有调用添加了库前缀,这样我就可以看到正在使用哪些网络库(igraph、网络等)。这些库中有很多重叠和相互依赖关系。
请注意,此代码应将 clr-degree
映射到 0-1
到 black
、度数 2
到 red
、度数 3
到 green
,以及 >=4
到 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()
产量: