O'Reilly logo

Graphing Data with R by John Jay Hilfiger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required