[R] Could graph objects be stored in a "two-dimensional list"?

3 views
Skip to first unread message

jpm miao

unread,
May 23, 2013, 11:30:11 AM5/23/13
to r-help
Hi,

I have a few graph objects created by some graphic package (say, ggplot2,
which I use frequently). Because of the existent relation between the
graphs, I'd like to index them in two dimensions as p[1,1], p[1,2], p[2,1],
p[2,2] for convenience.

To my knowledge, the only data type capable of storing graph objects (and
any R object) is list, but unfortunately it is available in only one
dimension. Could the graphs be stored in any two-dimensional data type?

One remedy that comes to my mind is to build a function f so that
f(1,1)=1
f(1,2)=2
f(2,1)=3
f(2,2)=4
With functions f and f^{-1} (inverse function of f) , the two-dimensional
indices could be mapped to and from a set of one-dimensional indices, and
the functions are exactly the way R numbers elements in a matrix. Does R
have this built-in function for a m by n matrix or more generally, m*n*p
array? (I know this function is easy to write, but just want to make sure
whether it exists already)

Thanks,

Miao

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Jeff Newmiller

unread,
May 23, 2013, 12:06:27 PM5/23/13
to jpm miao, r-help
You could use lists of lists, and index them with vectors.

a <- list()
a[[1]] <- list()
a[[2]] <- list()
a[[c(1,1)]] <- g11
a[[c(1,2)]] <- g12
a[[c(2,1)]] <- g21
a[[c(2,2)]] <- g22
print(a[[c(2,1)]])

but this seems like an inefficient use of memory because your indexed data is stored more compactly than the graph object is. I would index the data and generate the graph object on the fly when I wanted to see it.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdne...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

William Dunlap

unread,
May 23, 2013, 12:18:05 PM5/23/13
to Jeff Newmiller, jpm miao, r-help
>To my knowledge, the only data type capable of storing graph objects
>(and
>any R object) is list, but unfortunately it is available in only one
>dimension. Could the graphs be stored in any two-dimensional data type?

Lists can have any number of dimensions you want, just as with other vector
types. The default printout of such a thing is not very pretty, but the information
is in the object.

> M <- matrix(list(as.roman(99), "Two", c(3,pi) , c(4,44,444)), nrow=2, ncol=2)
> M
[,1] [,2]
[1,] 99 Numeric,2
[2,] "Two" Numeric,3
> M[[1,1]]
[1] XCIX
> M[[1,2]]
[1] 3.000000 3.141593

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

David Winsemius

unread,
May 23, 2013, 4:39:58 PM5/23/13
to jpm miao, r-help

On May 23, 2013, at 8:30 AM, jpm miao wrote:

> Hi,
>
> I have a few graph objects created by some graphic package (say, ggplot2,
> which I use frequently). Because of the existent relation between the
> graphs, I'd like to index them in two dimensions as p[1,1], p[1,2], p[2,1],
> p[2,2] for convenience.
>
> To my knowledge, the only data type capable of storing graph objects

(This will all be depending on what you do mean by "graph objects".)

> (and
> any R object) is list, but unfortunately it is available in only one
> dimension.

I think both of these presumptions are incorrect.

> Could the graphs be stored in any two-dimensional data type?
>
> One remedy that comes to my mind is to build a function f so that
> f(1,1)=1
> f(1,2)=2
> f(2,1)=3
> f(2,2)=4
> With functions f and f^{-1} (inverse function of f) , the two-dimensional
> indices could be mapped to and from a set of one-dimensional indices, and
> the functions are exactly the way R numbers elements in a matrix. Does R
> have this built-in function for a m by n matrix or more generally, m*n*p
> array? (I know this function is easy to write, but just want to make sure
> whether it exists already)
>
Matrices can hold list elements:

> matrix( list(a="a"), 2,2)
[,1] [,2]
[1,] "a" "a"
[2,] "a" "a"
> matrix( list(a="a"), 2,2)[1,1]
[[1]]
[1] "a"


And list may be nested in a regular "matrix"

> list( list( list(a="a"), list(b="bb") ),
list(list(c="ccc"), list(d="dddd") ) )[[1]][[2]]
$b
[1] "bb"


So storing in this manner for access by an appropriately designed function should also be straight-forward. You could argue that the lattice-object panel structure depends on this fact.

>
> [[alternative HTML version deleted]]
Please learn to post in plain text.

David Winsemius
Alameda, CA, USA
Reply all
Reply to author
Forward
0 new messages