File: graphAttributes.Rnw

package info (click to toggle)
r-bioc-graph 1.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,076 kB
  • sloc: ansic: 842; makefile: 12; sh: 3
file content (206 lines) | stat: -rw-r--r-- 6,072 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
%
% NOTE -- ONLY EDIT howtogenefilter.Rnw!!!
% Biobase.tex file will get overwritten.
%
%\VignetteIndexEntry{Attributes for Graph Objects}
%\VignetteDepends{graph}
%\VignetteKeywords{Graph}
%\VignettePackage{graph}
\documentclass{article}

\usepackage{hyperref}

\textwidth=6.2in
\textheight=8.5in
\oddsidemargin=.1in
\evensidemargin=.1in
\headheight=-.3in

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Rmethod}[1]{{\texttt{#1}}}
\newcommand{\Rcode}[1]{{\texttt{#1}}}
\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\classdef}[1]{%
  {\em #1}
}
\newcommand{\myincfig}[3]{\begin{figure}[htbp]
  \begin{center}
      \includegraphics[width=#2]{#1}
      \caption{\label{#1}#3}
  \end{center} \end{figure}}


\begin{document}
\title{Attributes for Graph Objects}
\author{Seth Falcon}
\maketitle

\section{Introduction}

The \Rpackage{graph} package provides representations of graphs (nodes
and edges) as S4 classes.  This vignette demonstrates how to add
arbitrary node and edge attributes to graph objects.

First, we create a graph to use as an example.  We will work with a
\Rclass{graphAM-class} instance, however, any subclass of
\Rclass{graph-class} would work. See Figure~\ref{foo}.

<<exampleGraph1, results=hide>>=
library("graph")
mat <- matrix(c(0, 0, 1, 1, 
                0, 0, 1, 1, 
                1, 1, 0, 1, 
                1, 1, 1, 0),
              byrow=TRUE, ncol=4)
rownames(mat) <- letters[1:4]
colnames(mat) <- letters[1:4]
@ 
<<exampleGraph2, print=TRUE>>=
g1 <- graphAM(adjMat=mat)
@ 

<<foo, fig=TRUE, prefix=FALSE, echo=FALSE, results=hide, include=FALSE, width=6, height=6>>=
if (require("Rgraphviz")) {
    gn = as(g1, "graphNEL")
    plot(gn, nodeAttrs=makeNodeAttrs(gn, shape="circle", fillcolor="orange"))
} else {
  plot(1, 1, main="Rgraphviz required for this plot")
}
@ 
\myincfig{foo}{0.33\textwidth}{The graph \Robject{g1}.}
\section{Edge Attributes}

\subsection{Default edge attributes}

All edges in a graph support the same set of attributes.  The set of
supported attributes can be defined and accessed using the
\Rmethod{edgeDataDefaults} method.  
A new graph instance will not have any edge attributes defined.
%
<<edgeDataDefaults1>>=
edgeDataDefaults(g1)
@ 

When a new edge attribute is defined, a default value must be
specified.  Here we will define two edge attributes: \Rcode{weight}
and \Rcode{code} and specify a default value for each one.

<<edgeDataDefaults2>>=
edgeDataDefaults(g1, "weight") <- 1
edgeDataDefaults(g1, "code") <- "plain"
edgeDataDefaults(g1)
@ 

The default value for a particular attribute can be obtained by
specifying the attribute name in the call to \Rmethod{edgeDataDefaults}.

<<edgeDataDefaults3>>=
edgeDataDefaults(g1, "weight")
@ 

\subsection{Getting edge attributes}

Edge attributes are set and accessed using the \Rmethod{edgeData}
method.  Only attributes defined using \Rmethod{edgeDataDefaults} can
be accessed using \Rmethod{edgeData}.  If an attribute has not be set
using \Rmethod{edgeData} for a given edge, then the default value is used.

<<edgeData1>>=
edgeData(g1, from="a", to="d", attr="weight")
edgeData(g1, from="a", attr="weight")
edgeData(g1, to="a", attr="weight")
allAttrsAllEdges <- edgeData(g1)
weightAttrAllEdges <- edgeData(g1, attr="weight")
@ 

\subsection{Setting edge attributes}

Attributes are set using the replacement form of \Rmethod{edgeData}.
This method allows the user to update the attribute for single edge,
set the attributes for a collection of edges to a single value, and to
set the attributes for a collection of edges to different values
specified by a vector of values.

<<edgeData2>>=
edgeData(g1, from="a", to="d", attr="weight") <- 2
edgeData(g1, from="a", attr="code") <- "fancy"
edgeData(g1, from="a", attr="weight")
edgeData(g1, from="a", attr="code")
@ 

We can set the attributes for multiple edges to a single value.

<<edgeData3>>=
f <- c("a", "b")
t <- c("c", "c")
edgeData(g1, from=f, to=t, attr="weight") <- 10
edgeData(g1, from=f, to=t, attr="weight")
@ 

It is also possible to set multiple attributes to different values in
a single call to \Rmethod{edgeData}.

<<edgeData4>>=
edgeData(g1, from=f, to=t, attr="weight") <- c(11, 22)
edgeData(g1, from=f, to=t, attr="weight")
@ 

Finally, we can set the an attribute to a vector of values by packing 
it into a list:
%
<<edgeData5>>=
edgeData(g1, from="a", to="d", attr="code") <- list(1:10)
edgeData(g1, from=f, to=t, attr="weight") <- mapply(c, f, t, "e", SIMPLIFY=FALSE) 
edgeData(g1, from="a", to="d", attr="code")
edgeData(g1, from=f, to=t, attr="weight")
@ 

\section{Node Attributes}

\subsection{Default node attributes}

Like edge attributes, all nodes in a graph support the same set of
attributes.  The supported set of attributes and their default values
is accessed using the \Rmethod{nodeDataDefaults} method.  The interface
is similar to \Rmethod{edgeDataDefaults}.

<<defaultNodeData1>>=
nodeDataDefaults(g1)
nodeDataDefaults(g1, attr="weight") <- 1
nodeDataDefaults(g1, attr="type") <- "vital"
nodeDataDefaults(g1)
nodeDataDefaults(g1, "weight")
@ 

As with edge attributes, default values are required for each node
attribute.  The default value is used as the node attribute for all
nodes in the graph that have not had their attribute value explicitly
set.  Attribute values can be any R object.

\subsection{Getting and setting node attributes}

Once a node attribute has been defined and given a default value using
\Rmethod{nodeDataDefaults}, individual node attributes can be accessed
using \Rmethod{nodeData}.

<<nodeData1>>=
nodeData(g1, n="a")
nodeData(g1, n="a", attr="weight") <- 100
nodeData(g1, n=c("a", "b"), attr="weight")
nodeData(g1, n=c("a", "b"), attr="weight") <- 500
nodeData(g1, n=c("a", "b"), attr="weight")
nodeData(g1, n=c("a", "b"), attr="weight") <- c(11, 22)
nodeData(g1, n=c("a", "b"), attr="weight")
@ 

<<other, hide=TRUE, echo=FALSE>>=
## We need to reconcile this
#g2 <- as(g1, "graphNEL")
#edgeWeights(g2)

@ 

\end{document}