R 如何建立基于相关性的网络?
我有一个矩阵如下R 如何建立基于相关性的网络?,r,networking,R,Networking,我有一个矩阵如下 data <- replicate(30, rnorm(30)) 数据你的问题很不具体。但像这样的事情应该让你开始: # Generate some toy data data <- replicate(30, rnorm(30)) library("igraph") # Load the igraph package corr <- cor(data) # Create weighted adjencency/correlation matri
data <- replicate(30, rnorm(30))
数据你的问题很不具体。但像这样的事情应该让你开始:
# Generate some toy data
data <- replicate(30, rnorm(30))
library("igraph") # Load the igraph package
corr <- cor(data) # Create weighted adjencency/correlation matrix
# Create a weighted complete graph from the correlation matrix
g <- graph.adjacency(corr, mode = "undirected", weighted = TRUE, diag = FALSE)
# Chose the layout function
custom.layout <- function(g, ...) {
# layout.drl(g, weights = E(g)$weight, ...) # For bigger graphs
layout.fruchterman.reingold(g, weights = E(g)$weight, ...)
}
现在它看起来不太像你展示的图表。首先,由于我们模拟玩具数据的方式,我们不期望有任何“中心”——一切都只是噪音。其次,布局高度依赖于布局功能。
第三,这只是为了让您了解如何定制图形和布局。看看igraph软件包
# Format edges
E(g)$cor <- E(g)$weight
E(g)$weight <- abs(E(g)$cor)
E(g)$color <- ifelse(E(g)$cor < 0, "blue", "red")
E(g)$width <- 3*atanh(E(g)$weight)
# Format vertices
V(g)$size <- 3*abs(rowSums(corr))
V(g)$color <- "grey"
V(g)$label.color <- "black"
V(g)$label <- ""
# Do the plot
plot(g, layout = custom.layout)