我正在尝试根据虚拟变量[color_thresh]为顶点着色。数据如下所示:
我已经尝试了几种建议的方法,但我能做的最接近(也是最简单的)产生以下内容:
以下是我的复制数据代码和上图:
library(igraph)
library(data.tree)
Level_0 = c("TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124")
Level_1 = c("","SC_776E_86","SS_A66D_9","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86")
Level_2 = c("","","","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42")
Level_3 = c("","","","","CORB_F2E4_17","CORB_F2E4_22","CORB_AE16_10921","CORB_AE16_10921","CORB_AE16_10921")
Level_4 = c("","","","","","","","CORB_D81E_45","CORB_D81E_45")
Level_5 = c("","","","","","","","","SC_B949_3156")
color_thresh = c(1,0,0,0,0,0,1,0,1)
dataset <- data.frame(Level_0,Level_1,Level_2,Level_3,Level_4,Level_5,color_thresh)
dataset$pathString <- paste(dataset$Level_0,
dataset$Level_1,
dataset$Level_2,
dataset$Level_3,
dataset$Level_4,
dataset$Level_5,
sep = "/")
g <- as.Node(dataset)
g <- as.igraph(g, directed=TRUE)
V(g)$color <- ifelse(dataset$color_thresh==1, "red", "green")
plot.igraph(g, vertex.label.color="black", vertex.label.dist=0, vertex.label.family="Helvetica", vertex.frame.color="white", vertex.size=25, layout=layout_as_tree)
当我按[color_thresh]对数据框进行排序时颜色会发生变化,但仍然没有给我正确的颜色。 任何评论帮助我理解条件着色如何工作非常感谢!非常感谢你。
答案 0 :(得分:1)
这是一种方法:
g <- as.Node(dataset)
g <- as.igraph(g, directed=TRUE)
检查节点的排序方式
g_data <- get.data.frame(g, what="vertices")$name
查看节点在数据中的排序方式:
data_vert <- apply(dataset[1:6], 1, function(x) tail(x[x!=""], 1))
根据节点对数据进行排序并应用颜色
V(g)$color <- ifelse(dataset$color_thresh[match(g_data , data_vert)] == 1, "red", "green")
plot.igraph(g, vertex.label.color="black", vertex.label.dist=0, vertex.label.family="Helvetica", vertex.frame.color="white", vertex.size=25, layout=layout_as_tree)
这是正确的颜色编码吗?
另一种方法是明确说明哪些节点应该具有哪种颜色:
g <- as.Node(dataset)
g <- as.igraph(g, directed = TRUE)
V(g)$color <- "green"
V(g)["TAC_310B_124"]$color <- "red"
V(g)["SC_B949_3156" ]$color <- "red"
V(g)["CORB_AE16_10921"]$color <- "red"
相同的输出图像