CHAPTER 3 HISTOGRAMS

Creating a Basic Histogram

par(mar = c(3, 3, 3, 3))
str(PlantGrowth)
## 'data.frame':    30 obs. of  2 variables:
##  $ weight: num  4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
##  $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
with(PlantGrowth, hist(weight))

Optional Parameters

par(mfrow=c(1,2),mar=c(5,4,4,4)) 
with(PlantGrowth,hist(weight))
with(PlantGrowth, hist(weight,
                       xlim= c(3,7),
                       ylim= c(0,10),
                       col="lightgrey",
                       border= "black",
                       main= "Histogram of Plant Weights",
                       xlab= "Dried weight (g)", 
                       ylab= "Frequency"))

## Changing Bin Sizes

set.seed(1234)
simulated_data <- data.frame(rnorm(n = 1000, mean = 500, sd = 50))
names(simulated_data) <- c("var_name")
str(simulated_data)
## 'data.frame':    1000 obs. of  1 variable:
##  $ var_name: num  440 514 554 383 521 ...
par(mfrow = c(2,2), mar = c(5,4,4,4))
with(simulated_data, hist(var_name, breaks = "Sturges", main = "Default bins"))

with(simulated_data, hist(var_name, breaks = "Scott", main = "Scott bins"))

with(simulated_data, hist(var_name, breaks = "FD", main = "FD bins"))

with(simulated_data, hist(var_name, breaks = (seq(from = 100, to = 700, by = 10)), main = "Custom "))

## Advanced Histogram Features

mean.weight <- with(PlantGrowth, mean(weight))
sd.weight <- with(PlantGrowth, sd(weight))
mean.weight
## [1] 5.073
sd.weight
## [1] 0.7011918
par(mar=c(5,4,4,4))
with(PlantGrowth, hist(weight,xlim = c(3,8),ylim = c(0,10), xaxs= "i", yaxs="i"))
abline(v= mean.weight, lty=1, lwd=3,col="blue")
abline(v= mean.weight - (sd.weight), lty=2, lwd=1, col="red")
abline(v= mean.weight + (sd.weight), lty=2, lwd=1, col="red")
legend("topright", 
       legend = c("Mean", "Mean +/- SD"),
       col = c("blue", "red"), 
       lwd = c(3,1), 
       lty = c(1,2))