Histograms

The hist() function

R offers several functions producing histograms. First the hist() function: it has a number of options to produc different types of histograms. Note that the histogram will produce a gaphical window.

- hist(urb) A simple histogram using default values.
- hist(urb, breaks=20) creates a histogram with 20 bins (classes, bars, intervals). breaks= can also point to a variable that contains the class (interval) borders, or the name of a method to determine the number of classes, namely "Scott" and "DF". [See help(hist) for more details]
- See help(hist) for further options
- If you wish to produce a histogram of all variables in a data frame, use the following syntax:
:
par (mfrow=c(5,5)) apply(world,2,hist)

The first command line defines a new graphical window that can contain 25 graphics (5 rows and 5 colums); if you omit this command, you will only see the histogram of the last variable. [Note that the par() function is use to define a wide variety of options for graphics]. - To produce a histogram for each continent use by(urb,continents,hist); as above you will need to use par() to define a graphics window than can contain as many graphics as they are continents.

Refinements

Add a dotplot to a histogram:

hist(urb) rug(urb)

rug() is one of the many functions that let you modify an existing graph, by adding further elements.

More...

- slider.hist(gnpcap) {aplpack} adds a slider that lets you interactively choose the number of bins
- histkdnc {descr} Histogram with kernel density and normal curve