R Network - 基于单独的向量对节点进行颜色编码

时间:2016-06-16 05:00:20

标签: r

我有一个矩阵B和一个向量A(矩阵的值为0或1;向量的值从0到1连续; a,b,c是3个属性):< / p>

B = matrix(c(0,1,0,1,0,1,1,1,0),nrow=3,ncol=3)
rownames(B)<-c("a", "b", "c")

A = c(0.23,0.56,0.78)

我尝试根据矢量A中的值创建一个网络,节点渐变色(比如说,比例是从白色到黑色,而不是属性c的值0.78比a暗,值0.23)。我试过这些线但是它们无法对节点进行颜色编码。

require(Rgraphviz)
am.graph<-new("graphAM", adjMat=B, edgemode="directed")
plot(am.graph, attrs = list(node = list(fillcolor = A),
                        edge = list(arrowsize=0.5)))

真的很感激任何帮助。我是R的新手,如果我的解释不明确,我真的很抱歉

1 个答案:

答案 0 :(得分:2)

您可以使用包igraph:

library(igraph)
B = matrix(c(0,1,0,1,0,1,1,1,0),nrow=3,ncol=3)
rownames(B)<-c("a", "b", "c")
A = c(0.23,0.56,0.78)*100
g<- graph.adjacency(B)

fine = 500 # this will adjust the resolving power.
palette = colorRampPalette(c('green','darkgreen'))
#this gives you the colors you want for every point
graphCol = palette(fine)[as.numeric(cut(A,breaks = fine))]
plot(g, vertex.size=A, vertex.color = graphCol)

如果您不想要不同大小的顶点,只需省略&lt; vertex.size = A&#39;在你的情节中!