使用ggnet2在R中按度着色

使用ggnet2在R中按度着色,r,ggplot2,igraph,graph-visualization,ggally,R,Ggplot2,Igraph,Graph Visualization,Ggally,我一直在尝试使用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 = vco

我一直在尝试使用
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)
lapply(c(“sna”、“间图”、“GGALY”、“IGRAPHE”、“网络”),要求,字符。仅=T)

数据我喜欢挑战,图表总是很有趣的。我想这就是你想要的(我修改了我的原稿,以使用你稍后在编写时提供的文件):

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

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

注:此代码应将
clr度数
映射到
0-1
黑色
,度数
2
映射到
红色
,度数
3
映射到
绿色
,度数
=4
映射到
红色

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()
库(ggplot2)
图书馆(igraph)
图书馆(GGALY)
#以下库也是必需的-内部使用
lapply(c(“sna”、“比例”、“图间”、“网络”),要求,字符。仅=T)
种子集(1234)
#数据来自https://snap.stanford.edu/data/ca-CondMat.html

数据您需要提供一个示例输入数据。这将使它更容易帮助您。这仍然是不可复制的,因为我们没有任何数据来运行它(我们没有访问“CA CondMat.txt”)。您应该按照我最初提供的链接中描述的方式提供数据。不要添加文件。链接可能会断开。请阅读@Mrflick给你的链接。仔细阅读也可能是个好主意。我已经想出了这样的办法。现在,我试图通过使用
size.cut
划分节点并给每个组指定一个特定的颜色来改进它,但我不知道如何做到这一点。好的,你能把这个标记为正确的(因为它回答了问题),然后问一个新的吗?这就是所谓的工作原理。