Boxplots With Point Identification and Different kind of boxplot

Created on 23 June 2013
Revised on Thu Aug 22 23:33:00 2013

original post is here and here

Boxplots With Point Identification

library(car)
## Loading required package: MASS Loading required package: nnet
Boxplot(~income, data = Prestige, id.n = Inf)  # identify all outliers

plot of chunk unnamed-chunk-1

## [1] "general.managers"         "lawyers"                 
## [3] "physicians"               "veterinarians"           
## [5] "osteopaths.chiropractors"
Boxplot(income ~ type, data = Prestige)

plot of chunk unnamed-chunk-1

## [1] "general.managers" "physicians"
Boxplot(income ~ type, data = Prestige, at = c(1, 3, 2))

plot of chunk unnamed-chunk-1

## [1] "general.managers" "physicians"
Boxplot(k5 + k618 ~ lfp * wc, data = Mroz)

plot of chunk unnamed-chunk-1

## [1] "746" "53"
with(Prestige, Boxplot(income, labels = rownames(Prestige)))

plot of chunk unnamed-chunk-1

## [1] "general.managers"         "lawyers"                 
## [3] "physicians"               "veterinarians"           
## [5] "osteopaths.chiropractors"
with(Prestige, Boxplot(income, type, labels = rownames(Prestige)))

plot of chunk unnamed-chunk-1

## [1] "general.managers" "physicians"

Basic boxplot

Add varwidth=TRUE to make boxplot widths proportional to the square root of the samples sizes. Add horizontal=TRUE to reverse the axis orientation.

boxplot(mpg ~ cyl, data = mtcars, main = "Car Milage Data", xlab = "Number of Cylinders", 
    ylab = "Miles Per Gallon")

plot of chunk unnamed-chunk-2

Notched Boxplot

Notched Boxplot of Tooth Growth Against 2 Crossed Factors. boxes colored for ease of interpretation

boxplot(len ~ supp * dose, data = ToothGrowth, notch = TRUE, col = (c("gold", 
    "darkgreen")), main = "Tooth Growth", xlab = "Suppliment and Dose")
## Warning: some notches went outside hinges ('box'): maybe set notch=FALSE

plot of chunk unnamed-chunk-3

In the notched boxplot, if two boxes' notches do not overlap this is ‘strong evidence’ their medians differ (Chambers et al., 1983, p. 62).

Colors recycle. In the example above, if I had listed 6 colors, each box would have its own color. Earl F. Glynn has created an easy to use list of colors is PDF format.

Other Options

The boxplot.matrix() function in the sfsmisc package draws a boxplot for each column (row) in a matrix.

The boxplot.n() function in the gplots package annotates each boxplot with its sample size.

The ** bplot()** function in the Rlab package offers many more options controlling the positioning and labeling of boxes in the output.

Violin Plots

A violin plot is a combination of a boxplot and a kernel density plot. They can be created using the vioplot() function from vioplot package.

library(vioplot)
## Loading required package: sm Package `sm', version 2.2-5: type help(sm)
## for summary information
## 
## Attaching package: 'sm'
## 
## 下列对象被屏蔽了from 'package:MASS':
## 
## muscle
x1 <- mtcars$mpg[mtcars$cyl == 4]
x2 <- mtcars$mpg[mtcars$cyl == 6]
x3 <- mtcars$mpg[mtcars$cyl == 8]
vioplot(x1, x2, x3, names = c("4 cyl", "6 cyl", "8 cyl"), col = "gold")
title("Violin Plots of Miles Per Gallon")

plot of chunk unnamed-chunk-4

Bagplot - A 2D Boxplot Extension

library(aplpack)
## Loading required package: tcltk
attach(mtcars)
bagplot(wt, mpg, xlab = "Car Weight", ylab = "Miles Per Gallon", main = "Bagplot Example")

plot of chunk unnamed-chunk-5