grap is a troff preprocessor that uses pic to construct different types of graphs. Its use in data analysis is limited. For example, you can use grap to determine whether a population is growing exponentially. grap, then, is used primarily to include
graphs in troff documents. Like the other troff preprocessors, it is fussy about syntax. You can't simply sit down and toss off three or four complicated graphs. You have to read the documentation. grap, like pic, faces stiff competition from the PC-based
One of the more endearing qualities of UNIX is its inconsistency. You would expect the grap macros to be .GS and .GE, and you would be wrong. The grap macros are .G1 and .G2. In addition to these macros, all that grap requires for a primitive graph is
some data. Take, for example, the grades on a test:
These dismal grades, after sorting, produce a scatter point graph.
Because grap has a copy facility similar to that of pic, you can simplify your code even more by putting the data in a separate file. (See Chapter 25.) For example,
If you want the graph to have a solid line instead of scatter points, simply add a line of code that says draw solid immediately after the .G1 macro.
You can make your graph much more attractive by drawing a frame, adding labels, and specifying ticks. The following code, for example, produces a more sophisticated graph.
frame invis ht 2 wid 3 left solid bot solid
label left "1990" "Dollars" left .5
label bot "Grand Total: $210,000"
ticks left out at 6000 "6,000", 9000 "9,000", 12000 "12,000", 15000 "15,000",\
18000 "18,000", 21000 "21,000"
ticks bot at 1990 "1990", 1995 "1995", 2000 "2000", 2005 "2005", \
Here, the frame is shown only on the bottom and left side. The x and y coordinates have labels. Also, the ticks have been specified explicitly; they are not determined by grap.
TIP: You can save yourself hours of debugging if you remember that grap doesn't understand commas in large numbers. The ticks left line above specifies 9000 "9,000". The commas are safely isolated
in labels specified in quotation marks. The grap specifications themselves contain no commas.
The data filein this case, cost.childalso must contain no commas.
NOTE: Earlier versions of grap may not recognize the abbreviation bot for bottom.
You can specify ticks as out. This means that the ticks themselves, but not their labels, appear outside the grap frame. You can also specify ticks as in, in which case they appear inside the frame.
If there are too many dates to fit across the bottom of a graph, you might want to use apostrophes in the labels, as in '10, '20, and '30. To do this, you must tell grap that your label is a literal. (This is what C programmers have to do all the time.)
If you want to specify the first and last dates in full, you need to use two tick lines. The following code, for example, produces bottom labels of 1900, '05, '10, '15, and so on, up to 1950:
ticks bottom out at 0 "1900", 50 "1950"
ticks bottom out from 05 to 45 by 5 "'%g"
NOTE: To suppress tick labels, use a null argument ("").
Notice the words at 0 in the previous example. grap recognizes x,y coordinates, and unlike pic, it understands that 0,0 is the intersection of the x and y axes. To use coordinates, you use the coord command, as in
coord x first-value, y last-value
Without the coord command, grap automatically pads your first and last values, giving you blank space at the beginning and at the end of your graph. coord suppresses padding.
Likewise, use coord if you want an exponential graph rather than a linear graph.
You can plot the figures for low-income and high-income families on the graph shown in Figure 26.1.
grap gives you a quick and relatively easy way of inserting graphs into memos or other text files. It can produce line and bar graphs, but no pie charts, no color, and no shading. Nevertheless, grap can be a useful tool. Like pic, grap is
system-sensitive, so allow time for debugging.