我使用visIgraph在R中绘制网络图。顺便说一下,我想把最暗的渐变放在节点数最多的组中,最轻的颜色和最少的节点数。
这是我的示例代码。
dat2<-data.frame(from=c("A",'B','C','E','F','H','J','K','L',
"M",'N','P','O','R','S','U'),
to=c("B",'D','D','G','G','I','I','I',"I",'N','O','Q','S','T','T','T'))
dat3 <- data.frame(source=dat2$from, dest=dat2$to)
g <- graph.data.frame(dat3)
clust <- clusters(g)
V(g)$color <- ifelse(clust$membership== as.numeric(names(which.max(table(clust$membership)))), "darkblue", "lightblue")
visIgraph(igraph=g,idToLabel=TRUE,layout = "layout_nicely")
我希望在应用渐变时图形看起来像这样。
答案 0 :(得分:0)
尝试类似
的内容clust <- clusters(g)
V(g)$color <- ifelse(clust$membership== as.numeric(names(which.max(table(clust$membership))) ), "darkblue", "lightblue")
visIgraph(igraph=g,idToLabel=TRUE,layout = "layout_nicely")
<强> 1。编辑多个组和着色顺序
必须添加一个排序,以便根据组大小以正确的顺序获得颜色
clust <- clusters(g)
colfunc <- colorRampPalette(c("lightblue", "darkblue"))
colorder <- setNames(c(as.numeric(names(sort(table(clust$membership))))), c(colfunc(clust$no)))
V(g)$color <- names(colorder)[match(clust$membership,colorder)]