Section 14 One Numeric: hist

14.1 Histogram

The generic function hist computes a histogram of the given data values.

14.2 Example 1

data(iris)

?hist

x <- iris$Sepal.Length
str(x)
 num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
range(x)
[1] 4.3 7.9
hist(x)

hist(x, breaks=15, xlim=c(4,8))

hist(x, breaks=15, xlim=c(4,8), freq=FALSE)

xbr <- c(4, 5, 5.5, 6, 6.5, 7, 8)
hist(x, breaks=xbr)

# Note: The y-axis label changed from 'Frequency' to 'Density'
# freq: Defaults to TRUE if and only if breaks are equidistant 
# (and probability is not specified).

hist(x, breaks=15, 
     xlim=c(4,8), freq=FALSE,
     main='Histogram of Sepal Length',
     xlab='Sepal Length (cm)',
     ylab='Density',
     axes=TRUE,
     col='orange',
     lty=1, border='purple')

# lines(density(x), col='red', lwd=2, lty=2)
# curve(dnorm(x, mean=mean(x), sd=sd(x)), add=TRUE)

xd <- hist(x, breaks=15, plot=FALSE)
xd
$breaks
 [1] 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4
[18] 7.6 7.8 8.0

$counts
 [1]  4  5  7 16 13  7 13 15  9 10 16  7 11  5  4  2  1  4  1

$density
 [1] 0.13333333 0.16666667 0.23333333 0.53333333 0.43333333 0.23333333
 [7] 0.43333333 0.50000000 0.30000000 0.33333333 0.53333333 0.23333333
[13] 0.36666667 0.16666667 0.13333333 0.06666667 0.03333333 0.13333333
[19] 0.03333333

$mids
 [1] 4.3 4.5 4.7 4.9 5.1 5.3 5.5 5.7 5.9 6.1 6.3 6.5 6.7 6.9 7.1 7.3 7.5
[18] 7.7 7.9

$xname
[1] "x"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

14.3 Example 2

data(warpbreaks)

  • Draw a histogram of the variable breaks

  • Discuss the histogram

  • Transform the data using log-transformation and redraw the histogram