Combining graphs with a common legend

A simple example - mainly graph commands with a dash of
graphics object manipulation

A common question is, "Can I combine graphs, but have a single, common legend
instead of each graph showing its own legend?". graph combine can't do
this, however, with our newfound tricks, we can.

A standard graph combine

First, let's do a standard graph combine of two graphs whose legend's
show the same information. (Click the following 5 commands.)

. sysuse citytemp , replace

. capture graph drop jan july

. scatter cooldd heatdd tempjan , name(jan)

. scatter cooldd heatdd tempjuly , name(july)

. graph combine jan july

That was what we expected. Let's drop the named graphs jan and july so we can
reuse the names. (Click the following command.)

. graph drop jan july

Create graphs without legends and combine them

Below, we have added the legend(off) option to our original scatters to
turn of the display of the legends. We then combine these graphs, being sure
they are on a single row and naming the resulting combined graph two.
(Click the next three commands.)

. scatter cooldd heatdd tempjan , legend(off) name(jan)

. scatter cooldd heatdd tempjuly , legend(off) name(july)

. graph combine jan july , rows(1) name(two)

Create a legend without a "graph"

That's nice - Stata let us create graphs without legends and combine them.
Now if only Stata would let us create legends without "graphs" we would be in
business. The graph command won't let us do this, but we can do it directly
through the graphics objects.

First, turn off everything the scatter command will allow by setting the
axes not to draw - options yscale(off) and xscale(off) and
name our graph leg_graph. (Click the command below.)

. scatter cooldd heatdd tempjan , yscale(off) xscale(off) name(leg_graph)

Now, if only we could get rid of the plotregion. It turns out
that almost all drawn objects have a setting .draw_view that can be
turned on and off. In fact, the yscale(off) and xscale(off)
options did nothing more than switch this setting to off (false). We can do
that by hand for the plotregion. Let's switch that setting to false for the
plotregion, minding recording our edit with _gm_edit. Note the name of
our object begins with .leg_graph and not .Graph because we named
this graph.(Click the next two commands.)

. _gm_edit .leg_graph.plotregion1.draw_view.set_false

. graph display

Make the legend "graph" non-stretchable

That's good, but our leg_graph still thinks it is a full graph. When
graphs are combined, they all expand or contract so that they each take up an
equal amount of space. We don't want our legend graph to do this because it
doesn't need as much space as our real graphs. The legend has a natural
height determined by the font size, key size, and number and position of the
keys; it doesn't need to expand to fill space. We can tell our legend graph
not to stretch in the Y-dimension, by changing its .ystretch setting.
This setting is not a style and is changed by the .set member function.
We will change the setting to fixed. (Click the next two commands.)

. _gm_edit .leg_graph.ystretch.set fixed

. graph display

Combine our legend-only "graph" onto the already combined graph

Now we can use the standard graph combine command to combine our
legend-only graph onto our previously created combined graph without legends,
being sure to stack the the two graphs in a single column with the legend_only
graph on the bottom. (Click the command below.)

. graph combine two leg_graph , cols(1)

Though we are new making changes directly in the graphics classes and that
took a while to explain, note that we used only two rather short graphics
class commands and the remainder of our work was done using standard
graph commands.

We could have made the graph somewhat prettier my setting some of the margins
to zero in the original graphs and combines, but we were aiming more for
clarity than looks.

The legend size is not really fixed

A better word for fixed when we set the Y-stretchability of our graph
would have been "non-stretchable". We did not fix the size in the
Y-dimension, instead we set the graph to take up "as much room as needed". We
can see this by expanding the font size for legend's key labels. (Click the
next two commands.)

. _gm_edit .Graph.plotregion1.graph2.legend.style.editstyle labelstyle(size(vlarge)) editcopy

. graph display

Note that what we have created is a complete graph able to interact be
saved, used, or even further combined. It can even be
re-schemed or further hand-edited. It is in every way a Stata graph.

<< index >>