You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在聚类后的网络可视化中保留顶点的原有颜色模型?

How to Preserve Original Vertex Colors When Plotting Clustered Networks in igraph?

The issue you're running into is that when you call plot() with a clustering object as the first argument, igraph's default behavior overrides your custom vertex colors to use the community-based coloring. Here are two straightforward fixes to keep your original vertex color scheme while still visualizing the clustering results:


Solution 1: Use mark.groups to Highlight Clusters (Keep Vertex Colors Intact)

This approach draws your original network with custom vertex colors first, then overlays semi-transparent markers to indicate which nodes belong to each cluster. This keeps your original color scheme front and center while still showing the clustering structure.

# Build your network and clustering (repeating your setup code for completeness)
MyEdges <- read.table(header=TRUE,sep=",",text=" from,to A,B C,A C,B D,A E,F F,G G,A G,E ")
MyNodes <- read.table(header=TRUE,sep=",",text=" name,group A,1 B,1 C,2 D,2 E,3 F,3 G,4 ")
MyNetwork <- graph.data.frame(d=MyEdges,vertices=MyNodes,directed=TRUE)

# Create your custom color palette
library(RColorBrewer)
coul <- brewer.pal(9, "Set1")
my_color <- coul[as.numeric(as.factor(V(MyNetwork)$group))]

# Run clustering
MyNetwork_cluster <- cluster_optimal(MyNetwork)

# Plot with original vertex colors + cluster markers
set.seed(1234)
plot(MyNetwork, 
     layout = layout.graphopt, 
     vertex.color = my_color,  # Your original vertex colors
     mark.groups = MyNetwork_cluster,  # Add cluster boundary markers
     mark.col = adjustcolor(coul[seq_along(MyNetwork_cluster)], alpha.f = 0.2),  # Semi-transparent markers
     mark.border = NA  # Remove marker borders for cleaner look
)

# Add your legend
legend("bottomleft", 
       legend=levels(as.factor(V(MyNetwork)$group)), 
       col = coul , 
       bty = "n", 
       pch= 20 , 
       pt.cex = 3, 
       cex = 1.5, 
       text.col=coul , 
       horiz = FALSE, 
       inset = c(0.1, 0.1) )

Solution 2: Explicitly Force Custom Vertex Colors in the Cluster Plot

If you prefer the default cluster plot styling (e.g., colored edges between communities), you can explicitly override the vertex color parameter by ensuring it's passed correctly to the plotting function. Some versions of igraph prioritize community colors, but explicitly setting vertex.color will override this:

# Reuse the same network/color/clustering setup as above

# Plot cluster results with custom vertex colors
set.seed(1234)
plot(MyNetwork_cluster, 
     MyNetwork, 
     layout = layout.graphopt,
     vertex.color = my_color,  # Force your original vertex colors
     edge.color = "gray"  # Optional: Gray edges to make vertex colors pop more
)

# Add your legend
legend("bottomleft", 
       legend=levels(as.factor(V(MyNetwork)$group)), 
       col = coul , 
       bty = "n", 
       pch= 20 , 
       pt.cex = 3, 
       cex = 1.5, 
       text.col=coul , 
       horiz = FALSE, 
       inset = c(0.1, 0.1) )

Why This Works:

When you plot a clustering object directly, igraph defaults to using community-specific colors for vertices. By explicitly defining vertex.color in either approach, you override this default behavior and retain your original group-based color scheme.

内容的提问来源于stack exchange,提问作者lukascbossert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:27:51