Search code examples
rdata-visualizationcluster-analysisigraph

Plot a "mini" graph from igraph object based on membership


I am using R. Suppose I have data (below) and make a graph:

library(igraph)
file <-data.frame(

"source" = c(
    "John",
    "John",
    "Tim",
    "Tim",
    "Alex",
    "Andrew",
    "Andrew",
    "Andrew",
    "Oliver",
    "Oliver",
    "Oliver",
    "Matt",
    "Steven",
    "Steven",
    "Steven",
    "Matt",
    "Charles",
    "Charles",
    "Charles",
    "Sean",
    "Ted",
    "Ryan",
    "Ryan",
    "Ryan",
    "Ted",
    "Phil",
    "Phil",
    "Phil",
    "Sam",
    "Toby",
    "Toby",
    "Donald",
    "Donald",
    "Donald",
    "Mitch",
    "Mitch",
    "Mitch"),

"target" = c("Sam",
             "Tim",
             "Alex",
             "Matt",
             "Andrew",
             "Sean",
             "Peter",
             "Ben",
             "Kevin",
             "Thomas",
             "Dave",
             "Steven",
             "Kenny",
             "Derek",
             "CJ",
             "Charles",
             "Ivan",
             "Kyle",
             "Andrew",
             "Ted",
             "Ryan",
             "Daniel",
             "Chris",
             "Scott",
             "Phil",
             "Henry",
             "George",
             "Paul",
             "Toby",
             "Donald",
             "Mitch",
             "Jack",
             "Luke",
             "Myles",
             "Elliot",
             "Harvey",
             "Owen")

)

graph <- graph.data.frame(file, directed=F)
graph <- simplify(graph)
plot(graph)

enter image description here

The graph has two distinct parts : "red" and "black".

Is there a way to only plot the "red" part? Is there a way to only plot the "black" part?

I tried the following code:

g1 = graph
cls = clusters(g1)
g2 <- delete_vertices(g1, V(g1)[cls$membership %in% which(cls$no =="1")])
plot(g2)

But this returns the original graph. Can someone please tell me what I am doing wrong? Should this be done with dplyr/sql instead?

Is it also possible to make a table that says: "black" has 4 observations, "red" has 34 observations?


Solution

  • Try this:

    g2 <- delete_vertices(g1, V(g1)[cls$membership == 2])
    plot(g2, main = "membership 1")
    
    g3 <- delete_vertices(g1, V(g1)[cls$membership == 1])
    plot(g3, main = "membership 2")
    

    enter image description here

    enter image description here

    Get counts per cluster:

    cls$csize
    # [1] 34  4
    table(cls$membership)
    #  1  2 
    # 34  4