Chapter 4 - Boxplots

4.1 Creating a Basic Boxplot

str(PlantGrowth)  # we have 30 observations, 2 variables
## '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 ...
#> '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)  # a summary for each variable in 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
#>      weight      group   
#>  Min.   :3.59   ctrl:10  
#>  1st Qu.:4.55   trt1:10  
#>  Median :5.16   trt2:10  
#>  Mean   :5.07            
#>  3rd Qu.:5.53            
#>  Max.   :6.31
par(mar = c(3, 3, 0, 3))  # (B,L,T,R)
# now create your box plot
with(PlantGrowth, boxplot(weight ~ group))  # long data, use ~ not ,

### 4.2 Optional Parameters

par(mfrow=c(1,1),mar=c(5,4,2,4))
with(PlantGrowth,boxplot(weight ~ group, 
     col= "gray",                        # box colour (US and UK spelling both work)
     border= "black",                    # box border     
     main= "Box Plot of Plant Weights",  # figure title
     xlab= "Treatment",                  # x-axis label
     ylab= "Dried weight (g)",           # y-axis label
     ylim= c(3,7),                       # y-axis range
     boxwex= 0.6))                       # set box widths (60% of default)

### 4.3 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))  # set horizontal to TRUE

### Advanced Boxplot Features

par(mfrow=c(1,1),mar=c(3,4,2,4))
#1: Reorder x-axis factor levels
# Note: data$variable is used to specify your data here, instead of with() 
PlantGrowth$group<-factor(PlantGrowth$group, levels= c("trt1", "trt2", "ctrl"))
# check the order has changed
summary(PlantGrowth$group)              
## trt1 trt2 ctrl 
##   10   10   10
#> trt1 trt2 ctrl 
#>   10   10   10
#2. create a plot 
with(PlantGrowth,boxplot(weight ~ group,
     col= c("grey70","grey70","grey40"),      # list colours to fill boxes
     names= c("Treatment 1", "Treatment 2","Control"), # list box names
     main= "Publication quality example",
     ylab= "Dried weight (g)",
     outpch=20,                           # change outlier symbol
     outcol="grey70",                     # change outlier colour
     ylim= c(3,7),                        # y-axis range
     boxwex= 0.6,                         # make the box width thinner
     las= 1))                             # set axis values to horizontal