[英]R igraph: Color nodes by degree?
# Erdos
par(mfrow=c(1,2))
g <- erdos.renyi.game(100, 1/100)
V(g)$size<-seq(0.05,5,0.05)
betweenness(g)
# Draw nodes and save positions
locs <- layout.fruchterman.reingold(g)
plot(g,
layout=locs,
vertex.label=NA,
main="Original",
vertex.color=degree(g))
g
vertex.color=degree(g)
did not work.不工作。 Could anyone tell me how to color the vertices by "degree"?谁能告诉我如何按“度”为顶点着色? Red (high value) to blue (low value) would be perfect.红色(高值)到蓝色(低值)将是完美的。
Thanks!谢谢!
A solution I found is to create a new color vector with the grey color R provides us with colors()[]
.我找到的一个解决方案是创建一个新的颜色向量,其中灰色 R 为我们提供了colors()[]
。 If you check colors()[]
in your terminal, you can see the full list of colors that are readable by the plot.igraph()
function.如果您在终端中检查colors()[]
,您可以看到plot.igraph()
函数可读的完整颜色列表。
You first charge your data (graph, etc.) :您首先对您的数据(图表等)收费:
edgelist <- read.csv(...)
graph <- make_graph_from_data(edgelist)
Then you create a vector of colors that corresponds to the length of your vertices list :然后创建一个与顶点列表长度相对应的颜色向量:
length(V(g)) # with a length of X vertices :
colors <- c(paste0(rep("grey",X),seq(X,1)))
Finally, you plot it with the attribute vertex.color
:最后,使用属性vertex.color
绘制它:
plot(g,vertex.color=colors[degree(graph)])
However, one can only use this little trick for graph with less than 100 values in degree(graph)
...但是,对于degree(graph)
少于100 个值的图,只能使用这个小技巧...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.