【问题标题】:R igraph: Color nodes by degree?R igraph:按度数着色节点?
【发布时间】:2021-07-31 01:22:42
【问题描述】:
# 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)

没用。谁能告诉我如何按“度数”为顶点着色? 红色(高值)到蓝色(低值)将是完美的。

谢谢!

【问题讨论】:

  • 你的意思是它不起作用,因为你的情节确实有不同程度的颜色?
  • 如果你想拥有从蓝色到红色的颜色,只需定义你自己的配色方案:例如。 d=degree(g); cols=setNames(colorRampPalette(c("blue", "red"))(length(unique(d))), unique(d)) 然后使用vertex.color=cols[degree(g)] 设置颜色。但是,这里的颜色范围可能不够:plot(1:6, col=cols, pch=16, cex=3),但只需更改 cols 向量以适合。
  • 这已经有所帮助。但看起来最大值以透明背景显示。其他一切都很好地从红色缩放到蓝色..
  • 是的,对不起...使用cols=setNames(colorRampPalette(c("blue", "red"))(length(unique(d))), sort(unique(d)))从零到上限排序,度数到字符vertex.color=cols[as.character(degree(g))]
  • 给出“seq.int(0, 1, length.out = n) 中的错误:'length.out' 必须是非负数另外:警告消息:在 .approxfun(x , y, v, method, yleft, yright, f) :“强制引入的 NAs”。cols[as.character(degree(g))] 看起来像: NA NA NA .. .

标签: r attributes igraph


【解决方案1】:

我找到的一个解决方案是创建一个新的颜色向量,其灰色 R 为我们提供colors()[]。如果您在终端中检查colors()[],您可以看到plot.igraph() 函数可读的颜色的完整列表。

您首先收取您的数据(图表等):

edgelist <- read.csv(...)
graph <- make_graph_from_data(edgelist)

然后创建一个与顶点列表长度相对应的颜色向量:

length(V(g)) # with a length of X vertices :

colors <- c(paste0(rep("grey",X),seq(X,1)))

最后,您使用属性vertex.color 绘制它:

  plot(g,vertex.color=colors[degree(graph)])

但是,这个小技巧只能用于degree(graph)中小于100个值的图形...

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2021-12-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多