I don't have the data

This example shows a little more of the underlying structure of graphs under
the guise of an interesting trick.

Assume you have been sent by email two Stata graphs as gph-files and that you
do not have the data from which the graphs were created. Let's clear
Stata back to a pristine state and graph use two saved gph-files by
clicking the next three commands in sequence.

. clear

. graph use heat , name(heat)

. graph use cool , name(cool)

What if we wanted to see both of these scatters on the same graph? We have
said that graphs are just composed of objects and that includes plots. What
if we just inserted the plot from one graph into the plotregion of the other
graph? Let's try. It turns out that we must inform the plotregion that it
is receiving a new plot and that is done with the .Declare member
program. Noting that we named our graphs cool and heat, we can
issue the following command to declare a new plot in .plotregion1 of the
.cool graph, naming that new plot plot2 and making the value of
that plot a reference to the plot on the .heat graph -
.heat.plotregion1.plot1. That is what the following command does.

. .cool.plotregion1.Declare plot2 = .heat.plotregion1.plot1.ref

We said that the .cool.plotregion1.plot2 would be a reference to our
the plot from .heat and that is what the .ref at the end of the
command designates. Without much explanation, will you accept that it is
almost always best to copy things around and among graphs by reference rather
than by value (not using the .ref suffix. This allows the objects to
retain their linkages to the scheme, because we are literally referring to
the same plot object.

Let's see our handiwork, by clicking on the command below.

. graph display

Well, that is pretty interesting, but much of the new plot is trailing off the
right of our graph. We told the plotregion of the .cool graph to add
this plot, however, the two graphs were on different scales and we did not
tell the plotregion to adjust its scale.

Plotregions are relatively smart, and are willing to loop through all of the
plots and adjust their scale to encompass the full range of what is being
plotted. They do this with the .reset_scales member program. (Click
next 2 commands.)

. .cool.plotregion1.reset_scales

. graph display

That is somewhat better. Their is nothing inaccurate about this graph.
Although the axes do not label the full range of values, what is labeled is
accurate and the plotregion now holds the full range of data.

We can tell the axes to reset themselves to a "natural" set of ticks and
labels, the same ones we would have gotten had we issued a graph command
with the two plots, with the .set_ticks member program. (Click the next
3 commands.)

. .cool.yaxis1.set_ticks

. .cool.xaxis1.set_ticks

. graph display

That looks pretty good. There is, however, still one thing missing. We now
have two plots, but no legend to identify them. The original graph suppressed
the legend, because there was only one plot. We can use our old friend, the
draw_view setting, to get inform that legend to draw itself. (Click the
2 commands below.)

. .cool.legend.draw_view.set_true

. graph display

Not quite what we wanted. The original legend knew only about the first plot.
We can tell it to search through all of its associated plotregions and
identify all of the plots with the .rebuild member program. Similarly,
we can ask it to find nice positions for the keys and labels of those plots by
issuing the .repositionkeys member program. To do that, and redisplay
the graph, click on the following 3 commands.

. .cool.legend.rebuild

. .cool.legend.repositionkeys

. graph display

There, we have pulled a plot from one graph to another and told the resulting
graph how to update itself to properly show the new plot.

I should note that the following things are true about the graph we have
created.

o Cannot be saved as a live graph

o Can be saved as an asis graph

o Can be printed, exported, combined, and copied to clipboard

Recall that live graphs contain a "story" of how they were
created and can be re-schemed when graph used. Even had we prefixed all
of the commands that we used above, this would not be sufficient to make our
graph a live graph. It is not possible to turn our new graph into a
live graph, just beyond the scope of this talk to cover the issues
involved.

There is a reason we could so easily pull a plot and its points
from one graph to another. The data for plots is stored in sersets. For more
information, click the digression below.

>> A digression on sersets

Let's prove that we can save and restore our create as an
asis graph by clicking the next 3 commands.

. graph save mygraph , replace asis

. discard

. graph use mygraph

<< index >>