graph_pairs: Turn pairs or paths into a graph

View source: R/zenpath.R

graph_pairsR Documentation

Turn pairs or paths into a graph

Description

Pairs are processed to produce a graph with the elements of the pairs as vertices and the pairs as undirected edges. The result can be displayed using plot().

Usage

graph_pairs(x, var.names = NULL, edgemode = c("undirected", "directed"))

Arguments

x

matrix or list of pairs along a zenpath. Can also be a list containing vectors representing paths in the graph. Every path must be of length at least 2 (i.e. each vector element of the list).

var.names

names of the variables appearing in x.

edgemode

type of edges to be used: either "undirected" (the default) or "directed" (in which case the order of the nodes in each pair matters).

Value

a graphNEL object; can be displayed using plot().

Note

zenplot() never use directed graphs nor graphs with isolated (disconnected) nodes.

Author(s)

Marius Hofert and Wayne Oldford

See Also

zenplot() which provides the zenplot.

Other tools related to constructing zenpaths: connect_pairs(), extract_pairs(), groupData(), indexData(), zenpath()

Examples

## To display the graphs constructed the packages
## graph and Rgraphviz packages need to be loaded
library(graph)
library(Rgraphviz)
##
## Get some pairs
pairs <- matrix(c(1,2, 5,1, 3,4, 2,3, 4,2), ncol = 2, byrow = TRUE)
g <- graph_pairs(pairs)
## which can be displayed using plot(g)
plot(g)

## Build a graph from a list of paths
paths <- list(3:1, c(3,5,7), c(1,4,7), c(6,7))
gp <- graph_pairs(paths)
## graph package draws with grid, so clear
grid.newpage()
plot(gp)

## Nodes do not need to be numbers
alpha_paths <- list(letters[3:1], letters[c(3,5,7)],
                    letters[c(1,4,7)], letters[c(6,7)])
grid.newpage()
plot(graph_pairs(alpha_paths))

## Zenplots never uses this feature but you could
## build a directed graph with a single isolated node
dg <- graph_pairs(alpha_paths,
                  var.names = c(letters[1:7], "ALONE"),
                  edgemode = "directed" )
grid.newpage()
plot(dg)


zenplots documentation built on Nov. 8, 2023, 1:10 a.m.