create two igraphs with edges and vertices and plot them both with ordered colours according to clusters in r

library(igraph)

# Create First Graph
vertices1 <- c("A", "B", "C", "D", "E")
edges1 <- c("A", "B", "B", "C", "C", "A", "D", "E")
g1 <- graph(edges1, vertices=vertices1)

# Create Second Graph
vertices2 <- c("W", "X", "Y", "Z")
edges2 <- c("W", "X", "X", "Y", "Z", "W")
g2 <- graph(edges2, vertices=vertices2)

# Create cluster memberships for the vertices
V(g1)$cluster <- c(1, 2, 2, 1, 1)
V(g2)$cluster <- c(1, 2, 2, 1)

# Merge the two graphs
g_merged <- disjoint_union(g1, g2)

# Plotting with ordered colors according to clusters
clusters_unique <- unique(c(V(g_merged)$cluster))
num_colors <- length(clusters_unique)
palette <- rainbow(num_colors)

plot(g_merged, vertex.color=palette[V(g_merged)$cluster])
713 chars
26 lines
main.r
0 chars
1 lines

related categories

gistlibby LogSnag