Skip to Main Content
Graphing Data with R
book

Graphing Data with R

by John Jay Hilfiger
October 2015
Beginner to intermediate content levelBeginner to intermediate
250 pages
6h 26m
English
O'Reilly Media, Inc.
Content preview from Graphing Data with R

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

A Course in Statistics with R

A Course in Statistics with R

Prabhanjan N. Tattar, Suresh Ramaiah, B. G. Manjunath

Publisher Resources

ISBN: 9781491922606Errata Page