Chapter 7. Histograms

Simple Histograms

Let’s revisit the sbp dataset from the multcomp package. A useful tool for examination of the blood pressure data is the familiar histogram. In this type of graph, the range of values of a numeric variable of interest (e.g., sbp) is usually laid out on the horizontal scale (x-axis). This scale is divided into sections, called bins.   The vertical scale (y-axis) shows how many observations fall into each bin.

Figure 7-1 was produced by calling the hist() function four times, once for each graph in the figure. Of course, you could have accomplished the same thing by typing several command lines at the console. The basic command is simply hist(sbp).

Histograms of the sbp variable from the sbp dataset, displaying the effect of differing numbers of bins.
Figure 7-1. Histograms of the sbp variable from the sbp dataset, displaying the effect of differing numbers of bins.

Here is the script to produce Figure 7-1:

# Script for Figure 7-1
library(multcomp)
attach(sbp)
par(mfrow = c(2,2))
hist(sbp, main = "a. Let R decide bin sizes")
hist(sbp, main = "b. Choose 4 particular breakpoints", 
  breaks = c(110,135,160,185))
hist(sbp, main = "c. Suggest number of breakpoints", 
  breaks = 30)
hist(sbp, main = "d. Set first, last and increment", 
  breaks = seq(110,190,15))
detach(sbp)

This code leaves the choice of number of bins to R, which is often a pretty good decision. In addition, each command line adds the argument main = to produce a title for that particular ...

Get Graphing Data with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.