Seth Falcon sfalcon at fhcrc.org
Wed Jun 7 16:09:42 CEST 2006

```Hi Mark,

"Kimpel, Mark William" <mkimpel at iupui.edu> writes:
> I am a new user to graph and Rgraphviz but until I updated to R 2.3.1
> edge weight labels over my output using code lifted from the Rgraphviz
> vignette. Now, after updating, the edge weights all come out
> "1". Below is some test code that mimics what I have been doing,
> i.e. coercing an adjacency matrix to a graphNEL object. The individual
> points in the matrix are the edge weights I want to have labeled.
>
> As this suddenly appeared after updating, I suspect a problem with the
> package graph.

Yes and no.  But first let me give you a different way to achieve your
goal and then some discusson...

> test.matrix<-matrix(rep(c(1,2,3,4), 9), ncol=6, nrow=6)
>
> rownames(test.matrix)<-colnames(test.matrix)<-c("a", "b", "c", "d",
> "e", "f")
>
> result.gN<-as(test.matrix, Class="graphNEL")

Instead of using the coerce method "as", you will have much more
control over the end result if you call new explicitly to create a
graph instance.  Here's what I mean:

## Use the graphAM class, it is an adjacency matrix representation and
## is easy to initialize given an adjacency matrix
g1 <- new("graphAM", test.matrix)
adjacency matrix must be symmetric for undirected graphs

## As you can see, the default is to create undirected graphs, but
## that isn't what you want if you have a nonsymmetric adj matrix.
## Let's try again:
g1 <- new("graphAM", test.matrix, edgemode="directed")
g1
A graphAM graph with directed edges
Number of Nodes = 6
Number of Edges = 36

## That looks good, but did we pick up the weights?
edgeWeights(g1)[1]
\$a
a b c d e f
1 1 1 1 1 1

## Not yet.  The graphAM class stores the adj mat as 0/1.  Upon
## initialization, you can tell the system how to interpret the matrix
## values and store them in an edge attribute of your choosing.
## The 'values' arg should be a length 1 list providing the name of
## the edge attribute and the default value for the matrix element
## values.
g1 <- new("graphAM", test.matrix, values=list(weight=1), edgemode="directed")
edgeWeights(g1)[1]
\$a
a b c d e f
1 3 1 3 1 3

## But you wanted a graphNEL.  That conversion is easy and does
## preserve node and edge attributes:
g2 <- as(g1, "graphNEL")
edgeWeights(g2)[1]

Some discussion
===============

1. We need to take a look at the Rgraphviz vignette and update it
where it makes sense to do so.

2. The past behavior of the matrix => graph as() methods is to create
undirected graphs.  For backwards compatibility, we should continue
with this.  But I think it makes more sense to create directed
graphs since they are more general.  I also think we should
encourage use of new() because it gives the user more control.

3. As for picking up the weights from a call to as(), again for
backwards compatibility we should probably just make this work.
Edge weights are special in that they are the most commonly used
edge attribute.

The new fangled node/edge attribute system treats all attributes
(almost) equally.  They are all stored in the same way and can all
be accessed using the same API (see the attributes vignette).
An edge attribute named "weight" is, in the current release,
slightly more equal :-)  The edgeWeights method looks for this
flexibility to edgeWeights to avoid lock-in to "weight" as a
special name and to make it easier to verify that the actual edge
weight values are of the expected type (some algorithms want
integers, for example).

ok, maybe I should stop there before this becomes a book.  Comments
and further questions welcome.

Best,

+ seth

```