[英]how to make a network based on correlation?
I have a matrix like below 我有一个如下的矩阵
data <- replicate(30, rnorm(30))
I would like to make a network like this image http://www.forwardprogress.net/data-steps-effective-relationship-marketing-dean-delisle/ 我想建立一个类似此图片的网络http://www.forwardprogress.net/data-steps-effective-relationship-marketing-dean-delisle/
of course the name of the variables (in this case V1 to V30 appeared) 当然,变量的名称(在这种情况下,出现了V1至V30)
is there any way to do it in R? 有什么办法可以在R中做到吗?
Thanks 谢谢
You're question is quite unspecific. 您的问题很不确定。 But something like this should get you started:
但是这样的事情应该可以帮助您入门:
# 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, ...)
}
Take a look at ?layout.fruchterman.reingold
and the other layout functions to tweak the layout. 看一看
?layout.fruchterman.reingold
和其他布局函数来调整布局。
# 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)
Now it doesn't look very much like the graph you present. 现在,它看起来与您呈现的图形不太相似。 First, we don't expect any "hubs" due to the way we simulate our toy data---everything is just noise.
首先,由于我们模拟玩具数据的方式,我们不希望任何“集线器”-一切都只是噪音。 Secondly, the layout is highly dependent on the layout function.
其次,布局高度依赖于布局功能。 Third, this was just to give you the idea of how to customise the graph and layout.
第三,这只是为您提供有关如何自定义图形和布局的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.