Hi:
On Sun, Feb 3, 2013 at 8:09 PM, Ista Zahn <
ista...@gmail.com> wrote:
> Hi, hope you don't mind my jumping in here.
Not at all. I appreciate it :)
>
> On Sun, Feb 3, 2013 at 10:04 PM, Mary <
mlho...@avalon.net> wrote:
>> Still curious, if we say,
>>
>> p <- ggplot(...
>>
>> Then what is p if not a pointer to a graph object?
>
> It _is_ a graph object
More specifically, saved ggplots are list objects of S3 class gg and
ggplot (which inherits from class gg). The structure of the ia object
in the initial example, associated with the Iowa map, is
str(state_seats[[1]])
List of 9
$ data :'data.frame': 991 obs. of 6 variables:
..$ long : num [1:991] -94.2 -94.2 -94.2 -94.5 -94.7 ...
..$ lat : num [1:991] 41.5 41.2 41.2 41.2 41.2 ...
..$ group : num [1:991] 1 1 1 1 1 1 1 2 2 2 ...
..$ order : int [1:991] 1 2 3 4 5 6 7 9 10 11 ...
..$ region : chr [1:991] "iowa" "iowa" "iowa" "iowa" ...
..$ subregion: chr [1:991] "adair" "adair" "adair" "adair" ...
$ layers :List of 2
..$ :Classes 'proto', 'environment' <environment: 0x0000000010eeaa08>
..$ :Classes 'proto', 'environment' <environment: 0x0000000010f72da0>
$ scales :Reference class 'Scales' [package "ggplot2"] with 1 fields
..$ scales: list()
..and 21 methods, of which 9 are possibly relevant:
.. add, clone, find, get_scales, has_scale, initialize, input, n,
.. non_position_scales
$ mapping :List of 2
..$ x: symbol long
..$ y: symbol lat
$ theme : list()
$ coordinates:List of 1
..$ limits:List of 2
.. ..$ x: NULL
.. ..$ y: NULL
..- attr(*, "class")= chr [1:2] "cartesian" "coord"
$ facet :List of 1
..$ shrink: logi TRUE
..- attr(*, "class")= chr [1:2] "null" "facet"
$ plot_env :<environment: R_GlobalEnv>
$ labels :List of 4
..$ x : chr "long"
..$ y : chr "lat"
..$ group: chr "group"
..$ label: chr "subregion"
- attr(*, "class")= chr [1:2] "gg" "ggplot"
Lists are the most general data structures in R. From this output, one
can see that there are nine components in the list object, several of
which are themselves lists. This is the actual object, not a pointer
to it.
This idea is not unusual in R: for example, any time you fit a model
to data, the returned model object is a list (or more likely, a list
of lists) with a specific class (e.g., lm, glm, loess), and there may
be methods defined for a given class to perform standard operations
such as print, plot or summary.
>
>
> No, I'm not getting this from SAS, but my formal computer science
> training, such as Pascal (which was about the the time S+ was written
> so I see similarities). I've taught computer science classes, such as
> data structures courses about queues. Am not as interested in
> SAS-like by statements as I am in creating a vector of graphs, such as
> p[1], p[2], for example 50 state graphs. But the below example
> actually calls the code to create the graph, rather than dealing with
> saved graphs each referenced by a vector element.
Exactly. This is one of several instances where the behavior of R
differs from that in more formal programming languages. It is,
however, a good idea to become very familiar with the str() function,
as it tells you the exact structure of your R object (data, model
output or function) and gives you some insight into how R manages
different types of data structures.
In R, pointers are generally associated with reference classes, which
are very recent to the language - IIRC, the R5 class debuted in R-2.14
(2011) and a new local reference class will premiere in the upcoming
R-3.0. The traditional classes (S3 and S4) both apply pass-by-value
semantics. gg and ggplot are instances of S3 classes.
>>
>> Is it not possible to have a vector of graph objects?
>
> Technically yes, because lists are vectors. But you cannot use an
> atomic vector, you must use a list.
> length(state_seats)
[1] 3
tells you that state_seats is a vector;
> class(state_seats)
[1] "list"
indicates that the object is a list;
> sapply(state_seats, class)
[,1] [,2] [,3]
[1,] "gg" "gg" "gg"
[2,] "ggplot" "ggplot" "ggplot"
tells you that each entity (i.e., list component) is an object of
class c("gg", "ggplot"). Further investigation with str() (see above)
indicates that each entity is in fact a list of lists containing
information about the various features of the object. A list object
can have arbitrarily many components and each component can have
literally any type of data structure in R.
>
> Once can say list(p,q,r), but I'm not sure what that graph object is
> if it is not a pointer.
It is a vector of gg objects, each of which is a list (of lists).
>
> Well I suppose at some level it is: the R language definition says
> "Note that in the C code underlying R, all objects are pointers to a
> structure with typedef SEXPREC; the different R data types are
> represented in C by SEXPTYPE, which determines how the information in
> the various parts of the structure is used.". But I'm not sure what
> practical importance this has to your original question.
>
>>
>> On the other thread, will try the double brackets in the morning and let you know.
>
> Yes, double brackets are the way.
>
> I am animating with saveHTML, but would rather have my graph objects
> created and tested rather than to call a function to create them right
> in the saveHTML(), but all the examples of doing this show calling the
> function that actually creates graphs, not cycling through existing
> graphs.
>
> The graphs don't exist as such until they are printed. Until then they
> are lists, with print methods that actually draws the graph.
Well stated.
>
> Hope this helps,
> Ista
>
Dennis