12.0 Introduction
12.1 Drawing a simple histogram
12.2 Activities
12.3 Labelling the bars
12.4 Activities
12,5 Drawing solid-looking histograms
12.6 Activities
12.7 Drawing two histograms together
12.8 Activities
Histograms provide a simple and dramatic way of displaying the relative frequencies with which events or items occur. In this chapter we show how to draw various types of histograms to display data. The programs rely on one of two procedures: PROChisto and PROC3Dhisto, which are two more of the procedures which we provides for you. PROChisto which draws up bars with suitable height and spacing for the data you supply and PROC3Dhisto which does the same, except that the bars are drawn with a mock perspective to give a three dimensional 'tower' effect. You must use the procedures with PROCscale so that your display automatically fits nicely onto the screen, and you can also optionally call on PRDCnamex, PROCnamey, PR0Cgraduate and PROC1abei. Like all our other procedures, PROChisto and PROC3Dhisto are listed in Appendix I and are also available, ready-recorded, on cassette tape.
A histogram shows the relative frequencies with which various events or items occur. Examples could be the numbers of cars, lorries, bicycles and pedestrians passing a road junction between certain hours; or the number of calories in different types of food. As an illustration, Screen Display 12.1 shows such a histogram for the cost of a holiday in an English resort, according to times of year. The histogram makes it is obvious at a glance that summer is the most expensive time, and that winter is the cheapest.
Listing 12.1 gives the program which produces Screen Display 12.1. It breaks naturally into three parts, namely: the input section to tell the computer how many bars are to be drawn and the frequencies that they are to represent; the scaling and axes-drawing section; and the histogram-drawing section, which uses the data and appropriate scaling to produce the display. The data could have been inputted in several ways. We chose to use the INPUT statement because it is a convenient and quick way of getting data into the program and allows a rapid presentation of any data in histogram form. (For displays which are to be recorded for later display, DAT A statements are preferable, as illustrated in our later examples.) The sections which call on PROCscale, draw the axes and draw the histogram are self-evident, because they rely on the procedures which we provide.
iRun the program of Listing 12.1 and input some data of your choice.
Screen Display 12.1 (first part)
Screen Display 12.1 (second part)
It is possible to label the bars of a histogram, but it is rather time-consuming because it involves a certain amount of trial, error and experiment. You will almost certainly only want to bother for a permanent display, in which case you will want to put in the data using DATA statements. Listing 12.2 and Screen Display 12.2 illustrate such a histogram. Several options are available for labelling the bars. For example, you can use the TAB function, as explained in Section 9.5; or you can issue the command VDU5 which makes all the printing which follows at a position dictated by the current graphics cursor. To print starting at the centre of the screen use:
VDU 5
MOVE 640,512
PRINT "message"
In Listing 12.2 we use the VDU5 statement as it is perhaps the simplest for you to modify for your own purpose.
Screen Display 12.2
Run Listing12.2 with some items of your own and experiment to get the best positioning for the labels of the categories.
Histograms can look particularly impressive if the bars are separated and appear to have depth. Such a histogram is demonstrated in Screen Display 12.3 which shows the numbers of students passing a certain exam during the years from 1970 to 1983. The dip around 1976 represents a fall in popularity of the subject. Listing 12.3 gives the program for the Screen Display.
i. Run the program of Listing 12.3, using your own data.
Screen Display 12.3
You can display the histograms for two sets of data together, so making it easier to compare and contrast them. The technique lends itself particularly to the separate-column, solid-looking type of display, of the previous section. Screen Display 12.4 illustrates the effect. The front bars show the frequency with which various heights occurred amongst female students and the back bars show the corresponding frequencies for the male students. The contrast is obvious at a glance and easily compensates for the greater difficulty in reading the heights from the scales.
The program, which is given in Listing 12.4, requires only slight modification from that of Listing 12.3. It uses a FOR...NEXT loop between lines 110 and 250. The origin of the graphics co-ordinates is reset between the two loops, using a VDU2 9,X;Y; command in line 160. This forces the origin for the rest of the graphics to be displaced to the point X,Y, so placing one set of bars slightly above and to the right of the other. This makes the second set of bars appear behind the first. As the second set of bars are drawn after and in front of the first, they hide the parts of the first which should be out of sight.
Screen Display 12.4
i. Run the program of Listing 12.4 with your own data.
ii. Try altering the VDU29;X;Y; statement to shift the displacement of one set of bars behind the other.