Bar? graphs

Going even further into the the system on which graph is built, let's
see how it is possible to add a whole new graph family to graph and have
graph use that family as its own. Official graph families, if you
recall, include twoway, bar and hbar, box and
hbox, dot, and matrix. Families are the word that follows
graph in a graph command.

We don't want to write lots and lots of code, so we will base our new family
on an existing family - bar.

A secret about graph

o designed to find properly named classes and use
them just like official graph families

o the proper name is <our_family>graph_g.class

- a class derived from the
official bar graph class

We will call our new family balbar and derive it from the existing class
that defines bar graphs. If you will click on the link
above (click the viewers Back button to
return), you will see the entire contents of the class-file that defines our
new graph family.

We won't go into too much detail about balbargraph_g.class, but there
are a few things to note. At the top of the file we see a class
statement that defines what member variables are included in our class and
some other aspects of the class. In this case, we see that no new member
variables are defined, but that the our class does inherit from the
bargraph_g class - the class that defines bar graphs.
Inheriting from bargraph_g means that our new class has all of the
member variables and member programs defined by bargraph_g. Because no
new variables are given in the class definition, we are not adding any member
variables.

This concept of inheritance allows us to leverage the capabilities of the
bargraph_g for our own purposes. While our balbargraph_g.class
file is a scant 57 lines long, the official bargraph_g.class is over
3,600 lines long and implements pretty much all of the features of
graph bar

Looking back at we see that it defines a single
member program draw_bar. In fact, it is redefining the draw_bar
graph from the bargraph_g.class and is thus changing how bars are drawn.
Every other feature of our new class is inherited without change from
bargraph_g.class

For more documentation on classes, see [P] class or in help
class.

We won't discuss the details of draw_bar. Essentially, it uses
low-level gdi calls to draw lines, points, symbols, and text. These
calls are are similar to the gph commands from pre-Stata8 graphics.
There are a few tricks relating to scaling because of the way symbols are
used, but most of the calls simply define how things look then draw the things
- lines and such. The entire purpose of draw_bar is to draw a
single bar given an x and y location for the middle of the bottom
of the bar and a size for the height.

Let's draw a simple bar chart using an extract from the US National
Longitudinal Survey of Women from 1988. We will use only a subset of
occupations so the graph is not too cluttered and the asyvar option so
that we get a little color. (Click the next 3 commands.)

. sysuse nlsw88 , clear

. keep if occ==3 | occ==5 | occ==6 | occ==8 | occ==11

. graph bar wage , over(collgrad) asyvar over(occupation)

Now, let's get a look at our new graph family in action, by clicking in
sequence on the following command. Note that the only change in the command
is switching balbar for bar.

. graph balbar wage , over(collgrad) asyvar over(occupation)

We claimed that our new family inherited a host of features from
bargraph_g.class. Let's see if that is true.

Let's add the percent and stack options to our command.

. graph balbar wage , over(collgrad) asyvar over(occupation) percent stack

We can indeed draw stacked percentage balloon graphs.

What if we were to draw our graph in a high wind.

. graph balbar wage , over(collgrad) asyvar over(occupation) horizontal

Because we are looking at labor data, let's try a another draw_bar
program. The also inherits from
bargraph_g.class, but draws a slightly more complicated "bar". Click on
the class file if you want to see its contents, then click on the two
graph stbar commands.

- another class derived from the
official bar graph class

. graph stbar wage , over(collgrad) asyvar over(occupation)

. graph stbar wage , over(collgrad) asyvar over(occupation) percent stack

<< index >>