在 igraph 中用作 vertex.color 时如何将因子级别更改为灰度

问题描述 投票:0回答:1

我正在使用一个因子在 igraph 中的不同顶点上色,但需要将其设置为黑白才能提交。我找到了这个answer,但它不起作用。以下是示例代码。

nodes <- c("A","B","C","D")
chars <- c(1,0,0,1)
nodelist <- as.data.frame(cbind(nodes, chars))
edges <- as.data.frame(rbind(c("A","B",1),c("A","C",0),c("A","D",0),c("B","C",1),c("B","D",0),c("C","D",1)))

chars <- as.factor(chars)
g <- graph.data.frame(edges, nodes, directed=FALSE)
plot(g, vertex.color = chars)

看起来不错,但需要黑白

colorset = c("0"='white',"1"='black')
plot(g, vertex.color = colorset)

白纸黑字,但它弄乱了因子水平

r graphics igraph
1个回答
0
投票

您可以为图形中的每个顶点分配特定属性。例如

chars <- as.factor(chars)
g <- graph.data.frame(edges, nodes, directed=FALSE)
colorset = c('white','black')
invcolorset = c('black', 'white')
V(g)$color <- colorset[chars]
V(g)$label.color <- invcolorset[chars]
plot(g)

© www.soinside.com 2019 - 2024. All rights reserved.