Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

r - Adjusting the node size in igraph using a matrix

I have the following network diagram:

set.seed(1410)
df<-data.frame(
"site.x"=c(rep("a",4),rep("b",4),rep("c",4),rep("d",4)),
"site.y"=c(rep(c("e","f","g","h"),4)),
"bond.strength"=sample(1:100,16, replace=TRUE))

library(igraph)
df<-graph.data.frame(df)
V(df)$names <- c("a","b","c","d","e","f","g","h")
layOUT<-data.frame(x=c(rep(1,4),rep(2,4)),y=c(4:1,4:1))
E(df)[ bond.strength < 101 ]$color <- "red"
E(df)[ bond.strength < 67 ]$color <- "yellow"
E(df)[ bond.strength < 34 ]$color <- "green"
V(df)$color <- "white"
l<-as.matrix(layOUT)
plot(df,layout=l,vertex.size=10,vertex.label=V(df)$names,
edge.arrow.size=0.01,vertex.label.color = "black")

enter image description here

and would like to adjust the size of the nodes using a matrix such as this:

m<-matrix(data=c(25,0,15,0,35,0,5,0,0,10,0,5,0,19,0,44), nrow=2, ncol=8)
colnames(m)<-c("a","b","c","d","e","f","g","h")
row.names(m)<-c("site.x","site.y")

        a  b  c d  e f  g  h
site.x 25 15 35 5  0 0  0  0
site.y  0  0  0 0 10 5 19 44

Any sugestions how I go about this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use

node.size<-setNames(c(25, 15, 35, 5, 10, 5, 19, 44),c("a", "b","c", "d", "e", "f", "g", "h"))
plot(df,layout=l,vertex.label=V(df)$names,
edge.arrow.size=0.01,vertex.label.color = "black",vertex.size=node.size)

so basically a named vector.

plot(df,layout=l,vertex.label=V(df)$names,
edge.arrow.size=0.01,vertex.label.color = "black",vertex.size=as.matrix(node.size) )

would also work

enter image description here

UPDATE:

If you need to use your m matrix

plot(df,layout=l,vertex.label=V(df)$names,
edge.arrow.size=0.01,vertex.label.color = "black",vertex.size=m[m!=0]) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...