简体   繁体   English

R列的颜色顶点

[英]R color vertices by column

I am using iGraph to plot graphs. 我正在使用iGraph绘制图表。 I have just two columns in my matrix- A and B. I need to color my nodes with just 2 colors – that indicates nodes that belong to A and those that belong to B. eg: 我的矩阵A和B中只有两列。我需要用2种颜色为节点着色 - 表示属于A的节点和属于B的节点。例如:

# k is a df with 2 columns – A and B
k_mx <- as.matrix(k)
k_mx_g <- graph.edgelist(k_mx, directed = FALSE)
V(k_mx_g)$color = ?? ( want blue for A and red for B)

Please let me know how to do this. 请让我知道如何做到这一点。

Thanks, PD 谢谢,PD

Assuming this dataframe: 假设这个数据帧:

k <-     structure(list(A = 1:4, B = 5:8), 
                 .Names = c("A", "B"), row.names = c(NA, -4L),
                  class = "data.frame")

... you could use rep with an each argument. ...你可以使用each参数的rep Otherwise they will be sequentially labelled with c("blue," "red", "blue", "red", "blue", "red", "blue", "red") due to argument recycling: 否则,由于参数回收,它们将依次用c标记(“蓝色”,“红色”,“蓝色”,“红色”,“蓝色”,“红色”,“蓝色”,“红色”):

V(k_mx_g)$color <- rep(c("blue", "red"), each=4)

Gabor's comment leads me to offer this option, which seems more general: Gabor的评论引导我提供这个选项,这似乎更通用:

V(k_mx_g)[k$A]$color <- "blue"
V(k_mx_g)[k$B]$color <- "red"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM