CHAPTER 4 BOXPLOTS

Creating a Basic Boxplot

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 ...
summary(PlantGrowth)
##      weight       group   
##  Min.   :3.590   ctrl:10  
##  1st Qu.:4.550   trt1:10  
##  Median :5.155   trt2:10  
##  Mean   :5.073            
##  3rd Qu.:5.530            
##  Max.   :6.310
par(mar = c(3, 3, 0, 3))
with(PlantGrowth, boxplot(weight ~ group))

Optional Parameters

par(mfrow=c(1,1),mar=c(5,4,2,4))
with(PlantGrowth,boxplot(weight ~ group, 
     col= "gray",
     border= "black", 
     main= "Box Plot of Plant Weights", 
     xlab= "Treatment", 
     ylab= "Dried weight (g)",
     ylim= c(3,7), 
     boxwex= 0.6))

## Horizontal Box Plots

par(mar=c(5,4,2,4))
with(PlantGrowth,boxplot(weight ~ group,
     col= "gray",
     main= "Horizontal Box Plot",
     xlab= "Dried weight (g)",
     ylab= "Treatment",
     horizontal = TRUE))

## Advanced Boxplot Features

par(mfrow=c(1,1),mar=c(3,4,2,4))
PlantGrowth$group<-factor(PlantGrowth$group, levels= c("trt1", "trt2", "ctrl"))
summary(PlantGrowth$group) 
## trt1 trt2 ctrl 
##   10   10   10
with(PlantGrowth,boxplot(weight ~ group,
     col= c("grey70","grey70","grey40"),
     names= c("Treatment 1", "Treatment 2","Control"), 
     main= "Publication quality example", 
     ylab= "Dried weight (g)", 
     outpch= 20,
     outcol= "grey70",
     ylim= c(3,7),
     boxwex= 0.6, 
     las= 1))