1
library("network")
library("networkD3")
library("igraph")

df1 <- read.table(text = "src   target
                  cllient1  cllient2
                  cllient1  cllient4
                  cllient1  cllient6
                  cllient2  cllient3
                  cllient4  cllient1
                  cllient4  cllient3
                  cllient5  cllient6
                  cllient6  cllient5", header = TRUE)

lesmis <- graph_from_data_frame(df1)
wc <- cluster_walktrap(lesmis)
members <- membership(wc)
lesmis <- igraph_to_networkD3(lesmis, group = members)

D3_network_LM <- networkD3::forceNetwork(Links = lesmis$links, Nodes = lesmis$nodes, 
                                         Source = 'source', Target = 'target', 
                                         NodeID = 'name', Group = 'group', 
                                         opacity = 1,zoom = TRUE)
networkD3::saveNetwork(D3_network_LM, "test.html", selfcontained = TRUE)

So close we got a network. After that we create new graph by merging several vertices into one. In our case those vertices that belong to particular community.

lesmis <- graph_from_data_frame(df1)
cg <- contract.vertices(lesmis, members)
ay <- as_long_data_frame(cg)
View(ay)

we got new graph,

from    to
1   2

We may built again new graph where now these nodes are the groups, but their names are 1 and 2, my question is how we can add clients names to this new graph. So that, when hovering , we may get not only the number of the node(group) but also the list of client that belongs to this new node(group).

Maksym Moroz
  • 306
  • 2
  • 14
  • 1
    I think here `graph_from_data_frame(bank)` you mean `graph_from_data_frame(df1)` to make the example work, and the second part imho is not working, having `Error in contract.vertices(lesmis, members) : Not a graph object`. – s__ Nov 20 '18 at 15:14
  • 1
    Have edited, thank you – Maksym Moroz Nov 21 '18 at 07:20

1 Answers1

0

Your example code doesn't work for multiple reasons (I edited it a bit to fix some obvious problems), but I think the following achieves what you're trying to do...

library(network)
library(networkD3)
library(igraph)

df <- read.table(header = TRUE, 
                 text = "src   target
                         cllient1  cllient2
                         cllient1  cllient4
                         cllient1  cllient6
                         cllient2  cllient3
                         cllient4  cllient1
                         cllient4  cllient3
                         cllient5  cllient6
                         cllient6  cllient5")

df_graph <- graph_from_data_frame(df)

wc <- cluster_walktrap(df_graph)
members <- membership(wc)

df_graph_cntrctd <- contract(df_graph, members, vertex.attr.comb = toString)

df_graph_cntrctd_D3 <- igraph_to_networkD3(df_graph_cntrctd)

networkD3::forceNetwork(Links = df_graph_cntrctd_D3$links, 
                        Nodes = df_graph_cntrctd_D3$nodes, 
                        Source = 'source', Target = 'target', 
                        NodeID = 'name', Group = 'name', 
                        opacity = 1, zoom = TRUE)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • yep, sorry for that silly mistakes.. Your suggestion solves what I was asking about :) Thanks a lot. – Maksym Moroz Nov 21 '18 at 07:18
  • Can you edit your original question with a reproducible example and/or the exact error message? – CJ Yetman Nov 21 '18 at 09:49
  • But as my real graph is more bigger, unfortunately I got an error after this line of code df_graph_cntrctd_D3 <- igraph_to_networkD3(df_graph_cntrctd) Cannot allocate vector on some point because of lack of ram memory I suppose. Is there possible way to use my hdd memory to solve this problem? – Maksym Moroz Nov 21 '18 at 09:53
  • Error: cannot allocate vector of size 40 Kb – Maksym Moroz Nov 21 '18 at 10:00
  • So I have more clients names and the length of name of each client in every group is bigger than in our example, therefore my RAM ends up quickly – Maksym Moroz Nov 21 '18 at 10:48
  • I can't really solve that problem without being able to reproduce it myself. I can easily forewarn you though that `d3.forceSimulation`, and therefore `networkD3::forceNetwork()`, is very inefficient for plotting more than a couple hundred nodes. Sounds like you're going to have a lot more than that. – CJ Yetman Nov 21 '18 at 12:05
  • 1
    yes, I agree. I wrote df_graph_cntrctd <-as.undirected(df_graph_cntrctd) and it accomplished well. And then used ability from your package about alert window, from here https://stackoverflow.com/questions/52477969/r-networkd3-click-action-to-show-information-from-node-data-frame/52480381#52480381 to show the list in the box not just in line. Thanks a lot for your help and such nice function :) – Maksym Moroz Nov 21 '18 at 12:39