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
packages.
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:
.G1
75
78
81
52
63
61
70
71
84
58
.G2
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,
.G1
copy "test.scores"
.G2
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", \
2010 "2010"
draw solid
copy "cost.child"
.G2
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 does more than just draw lines. It can print a grid. It can draw a circle, ellipse, or arrow. In fact, grap can draw just about anything that pic can.
grap has a macro facility just like that of pic:
define name X commands X
For the delimiter, you can use any character that isn't in the command text. You can also use braces. grap permits up to nine arguments, just as pic does.
In addition to data explicitly specified in a file, grap works with functions that more or less describe the data. The following code, for example, produces a sine curve:
frame ht 1 wid 3
draw solid
pi=atan2(0,-1)
for i from 0 to 2*pi by .1 do { next at i, sin(i) }
Table 26.1 summarizes the grap commands. Square brackets indicate that an argument is optional. A pipe between arguments means that you must use only one of the arguments.
Table 26.1. Summary of grap commands.
Command
Syntax
Description
frame
frame [ht expr] [wid expr] [[side] [descr]]
Specifies the dimensions for the frame drawn around the graph
side
top|bot|right|left
Refers to the frame
descr
solid|invis|dotted|dashed
Describes the lines used to draw the frame. You can control dotting and dashing by specifying the distance between dots or the length of and distance between dashes.
label
side list
Specifies the placement of and the text for labels
shift
left|right|up|down expr
Specifies the shift
list
rjust|ljust, above|below, size expr
Encloses items in quotation marks. You can modify the placement. size expr reduces the point size. This is useful for labels or for putting words or symbols in the graph itself.
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.