igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Plotting a signed and directed graph


From: Gábor Csárdi
Subject: Re: [igraph] Plotting a signed and directed graph
Date: Tue, 14 Apr 2009 12:42:31 +0200

Yes, read.graph does not support reading 'signed' graphs. The thing is
that the Pajek format is not documented and I have never seen a signed
format so far.

Here is a workaround, we need to read in the file "by hand":

## this ignores the -1 distinction edges but reads the vertex
attributes at least
g <- read.graph("/tmp/signed.net", format="pajek")

## by hand, only the lines after "*Matrix"
lines <- readLines("/tmp/signed.net")
matline <- grep("^.Matrix", lines)
lines2 <- paste(collapse="\n", lines[(matline+1):length(lines)])

A <- matrix(scan(textConnection(lines2)), byrow=TRUE, vcount(g))

g2 <- graph.adjacency(A, weighted=TRUE)
E(g2)$sign <- E(g2)$weight
V(g2)$id <- V(g)$id
g <- g2

Btw. the graph is quite dense, you might want to plot it with
tkplot(), adjust the vertex positions and then get the new coordinates
with tkplot.getcoords(). But this works only if you have a couple of
graphs of course.

Best,
Gabor

On Tue, Apr 14, 2009 at 12:26 PM, Tom Backer Johnsen
<address@hidden> wrote:
> Gabor:
>
> Your example was very nice, but when reading the graph it failed.  I suspect
> the error is in read.graph, but as far as I can see from the documentation,
> there are no arguments to tell the function that the graph is signe either.
>  I used the following commands in R:
>
> library(igraph)
> g <- read.graph (file="First.net", format="pajek")
> g <- as.directed (g)
> E(g)$curved <- is.mutual(g)
> # -1 edges are dashed
> E(g)$lty <- ifelse( E(g)$sign > 0, 1, 2)
> # A nice layout
> coords <- layout.fruchterman.reingold(g)
> plot(g, layout=coords)
>
> And the First.net file looks like this:
>
> *NETWORK First.net; 14.04.2009 / 09:46:56
> *Vertices 10
> 1 "S65"
> 2 "S29"
> 3 "S04"
> 4 "S75"
> 5 "S24"
> 6 "S81"
> 7 "S51"
> 8 "S78"
> 9 "S86"
> 10 "S39"
> *Matrix
>  0 0 0 0 0 1 0 0 0 -1
>  0 0 1 1 0 1 1 0 1 0
>  -1 0 0 1 0 0 1 0 1 0
>  -1 1 0 0 1 1 1 0 1 0
>  0 1 0 1 0 0 0 0 -1 -1
>  1 -1 0 0 0 0 0 1 0 0
>  0 -1 1 1 0 -1 0 0 1 0
>  0 0 1 1 0 1 -1 0 1 1
>  0 0 0 0 0 0 0 0 0 -1
>  1 1 1 1 1 1 1 1 1 0
>
> Tom
>
>
> Tom Backer Johnsen wrote:
>>
>> Thanks for the detailed response!  And fast as well.  I'll look into it.
>>
>> Tom
>>
>> Gábor Csárdi wrote:
>>>
>>> Tom,
>>>
>>> it is possible, with 0.5.2 or later versions. (Because of the curved
>>> edges.) 0.5.2 will be on CRAN at any time, in the meanwhile it is
>>> here:
>>>
>>> http://cneurocvs.rmki.kfki.hu/igraph/download/igraph_0.5.2.tar.gz
>>> http://cneurocvs.rmki.kfki.hu/igraph/download/igraph_0.5.2.zip
>>>
>>> The latter is for windows. Here is how to do it:
>>>
>>> library(igraph)
>>>
>>> # just to always get the same nice picture
>>> set.seed(123)
>>>
>>> # a small directed random graph to test
>>> g <- random.graph.game(10, 20, type="gnm", directed=TRUE)
>>>
>>> # signs are random, too
>>> E(g)$sign <- sample( c(-1,1), ecount(g), replace=TRUE)
>>>
>>> # mutual edges are curved
>>> E(g)$curved <- is.mutual(g)
>>>
>>> # -1 edges are dashed
>>> E(g)$lty <- ifelse( E(g)$sign > 0, 1, 2)
>>>
>>> # A nice layout
>>> coords <- layout.fruchterman.reingold(g)
>>>
>>> # And the plot
>>> plot(g, layout=coords)
>>>
>>> Best,
>>> Gabor
>>>
>>> On Tue, Apr 14, 2009 at 10:43 AM, Tom Backer Johnsen
>>> <address@hidden> wrote:
>>>>
>>>> Gentlepersons:
>>>>
>>>> I would like to obtain a plot of a signed and directed graph.  The graph
>>>> is
>>>> small, 10 nodes or vertices.  Since the graph is directed, I would like
>>>> to
>>>> have a curved arrow if there are arcs in both directions, with
>>>> continuous
>>>> leines and dashed for the negative ones. After reading the graph
>>>> (containing
>>>> -1's zeroes, and +1's) I see that I can plot a directed graph using g <-
>>>> as
>>>> directed (g) and then plot (g).
>>>>
>>>> Is this possible?
>>>>
>>>> Tom
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> igraph-help mailing list
>>>> address@hidden
>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>>
>>>
>>>
>>>
>>
>>
>
>
> --
> +----------------------------------------------------------------+
> | Tom Backer Johnsen, Psychometrics Unit,  Faculty of Psychology |
> | University of Bergen, Christies gt. 12, N-5015 Bergen,  NORWAY |
> | Tel : +47-5558-9185                        Fax : +47-5558-9879 |
> | Email : address@hidden    URL : http://www.galton.uib.no/ |
> +----------------------------------------------------------------+
>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM




reply via email to

[Prev in Thread] Current Thread [Next in Thread]