Learn R Programming

RnavGraph (version 0.1.8)

newgraph: Create a graph object of class graph

Description

Create a graph object of class graphNEL or graphAM. You might also use the graph creating facility provided by the graph package.

Usage

newgraph(nodeNames, mat, weights = NULL, directed = FALSE, isAdjacency = FALSE, ...)

Arguments

nodeNames
Numeric or character string vector.
mat
Either an adjacency matrix or a from to matrix.
weights
Numeric weights for edges. Either in the same order as the from to matrix or as a square matrix, depending what one have chosen for the mat argument.
directed
Logical value for defining a directed or undirected graph.
isAdjacency
If argument mat is adjacency matrix.
...
Currently not needed.

Value

graphNEL or grapAM object.

See Also

navGraph, completegraph, linegraph

Examples

Run this code
## Using from to matrices
from <- c("A","A","C","C")
to   <- c("B","C","B","D")
ftEmat <- cbind(from,to)

## note how the E node is added
G <- newgraph(nodeNames = LETTERS[1:5], mat = ftEmat)

## say you would like to add weights to the graph
weights <- c(2,1,3,4)
G <- newgraph(nodeNames = LETTERS[1:5], mat = ftEmat, weights = weights)

## newgraph with adjacency matrix
V <- c('s.L', 's.W', 'p.L', 'p.W')
adjM <- matrix(c(0,1,1,0,1,0,1,1,1,1,0,0,0,1,0,0), ncol = 4)
all(adjM == t(adjM)) ## is symmetric (undirected)
G <- newgraph(nodeNames = V, mat= adjM, isAdjacency=TRUE)

## if you use adjacency matrices, you can add a matrix with weights
adjM <-     matrix(c(0,0,1,0,1,0,1,1,0,0,0,0,0,1,0,0), ncol = 4)
weightsM <- matrix(c(0,0,5,0,2,0,1,3,0,0,0,0,0,7,0,0), ncol = 4)
G <- newgraph(nodeNames = V, mat= adjM, weights = weightsM, directed = TRUE, isAdjacency=TRUE)
edgeData(G, attr = "weight")

Run the code above in your browser using DataLab