Search code examples
rnetwork-programmingplotcluster-analysisigraph

How can I add edges into an existing plot?


I am wanting to plot graph clusters that I define by myself. I am using the simplified undirected enron data.

library(igraphdata)
data("enron")
g <- as.undirected(enron)
g <- simplify(g)
rm("enron")

member <- c(1,  8,  9,  9, 10, 10,  8,  7,  4,  1,  2,  6,  3,  1,  2,  8,  7,  2,  1,  5,  
            1,  7,  6,  4,  8,  4,  8, 10, 3,  6,  1,  4,  7,  4,  3,  7,  9, 10,  3,  8,  1,
            9,  8,  2,  7,  2,  9,  5,  1,  2,  6, 10,  3,  3, 2,  1,  9, 10,  3,  5,  6,  5,
            5,  3,  7,  6,  9, 10,  8, 10,  8,  8, 10, 10, 10,  8,  7,  7,  9,  1,  9, 2,  9,
            7,  2,  7,  7,  3,  2,  5,  2,  1,  6,  5, 10,  4,  3,  2,  4,  6,  4,  9,  5,  4,
            1, 10,  2,  3, 4,  3,  6,  3,  6,  4,  6,  8,  2,  4,  5,  1,  5,  1,  4, 10,  4,  7,
            5,  9, 10,  1,  2,  1,  5,  7,  5, 3,  5,  8, 7,  9,  5,  8,  1,  5,  3,  3,  3, 10,  
            1,  7,  8,  4,  1, 10,  9,  6,  9,  9,  4,  2,  6,  4, 6,  3,  5,  6,  9,  7,  6,  6,  
            4,  8,  6,  8,  8,  2,  5,  4,  3,  2,  9, 10,  2,  7)

I have tried many ways but none looks good. The best I can make is

edges_data_frame <- get.data.frame(g, what = "edges")
w.mem <- rep(0, length(E(g)))
for (i in 1:length(E(g))){
  w.mem[i] <- ifelse(member[edges_data_frame$from[i]] == member[edges_data_frame$to[i]], 500, 1)
}

mem <- make_clusters(g,member)
E(g)$weight <- w.mem

colors <- rainbow(max(membership(mem)))
layout <- layout.fruchterman.reingold(g, weights=w.mem)

set.seed(1234)
plot(g, vertex.color=colors[mem$membership], 
     mark.groups=communities(mem),
     vertex.label = NA, 
     edge.width = 1, edge.color = "lightgray", vertex.size = 5)

my first trial

I found that the "deleting edges plot" looks much cleaner

coGrph <- delete_edges(g, E(g)[crossing(mem, g)])
col_vector <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000')
temp <- sapply(1:length(V(g)), FUN = function(i) {col_vector[member[i]]})

V(coGrph)$color <- temp
plot(coGrph, vertex.label = NA, vertex.size = 5)

my second trial

However, this plot has some missing edges and does not reflect the true connection of the plot. I want to use this plot and add the deleted edges back to the plot without changing the positions I have right now. Is it possible?

Thank you very much I really appreciate your help.


Solution

  • Yes. Use your coGrph to create a layout, but then plot the original graph.

    Continuing your "second trial"

    set.seed(1234)
    LOcG = layout_nicely(coGrph)
    
    V(g)$color <- temp
    plot(g, layout=LOcG, vertex.label = NA, vertex.size = 5)
    

    Graph with all edges.