[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] About plotting communities calculated with outside function
From: |
Tamás Nepusz |
Subject: |
Re: [igraph] About plotting communities calculated with outside functions |
Date: |
Mon, 19 Mar 2012 19:58:03 +0100 |
> I am using a self-made function to identify communities in a network.
> With this function I obtain in which community each of my vertices are
> in (just like a $membership does). I would like to plot my network
> representing this communities, just like we can do with the function
> plot when we use variable of class "communities".
Try this:
> net <- graph.famous("Zachary")
> members <- rep(1:2,17)
> comms <- list(membership=members, vcount=vcount(net),
> algorithm="my.fancy.algorithm")
> class(comms) <- "communities"
> plot(comms, net)
The explanation is as follows. R variables may have an associated "class", and
the method dispatch mechanism in R is influenced by the class of the first
argument to a generic function. When you call plot(), R will examine the class
of the first argument and it may call a "specialized" version of plot() if the
first argument is of a given class. In particular, if the first argument has
class "communities", plot() will forward the call to plot.communities() and
then you get the fancy display that you have already seen.
Now, the only thing we need is to construct an R object that "looks like" a
member of the "communities" class. To figure out how such a class looks like,
let us take a look at the output of a community detection method, say, walktrap:
> wc <- walktrap.community(net)
> wc2 <- unclass(wc)
> wc2
unclass() simply removes the class attribute from the wc object (so it becomes
a "generic" R variable), so you can see that you practically need an R list
with as many of the following members as possible:
$membership - the membership vector
$vcount - the number of vertices in the graph
$algorithm - the name of the algorithm that produced the clustering
$modularity - the modularity of the clustering, or the modularity after each
split if this is a hierarchical clustering
$merges - the order in which nodes are merged in a hierarchical clustering
So, we can simply construct a list with the appropriate members manually
(that's what I do in the first code snippet with the list() function), assign
it to the "communities" class, and then the R method dispatch mechanism will do
its magic.
Best,
Tamas