Created on 23 June 2013
Revised on Thu Aug 22 23:33:00 2013
original post is here and here
library(car)
## Loading required package: MASS Loading required package: nnet
Boxplot(~income, data = Prestige, id.n = Inf) # identify all outliers
## [1] "general.managers" "lawyers"
## [3] "physicians" "veterinarians"
## [5] "osteopaths.chiropractors"
Boxplot(income ~ type, data = Prestige)
## [1] "general.managers" "physicians"
Boxplot(income ~ type, data = Prestige, at = c(1, 3, 2))
## [1] "general.managers" "physicians"
Boxplot(k5 + k618 ~ lfp * wc, data = Mroz)
## [1] "746" "53"
with(Prestige, Boxplot(income, labels = rownames(Prestige)))
## [1] "general.managers" "lawyers"
## [3] "physicians" "veterinarians"
## [5] "osteopaths.chiropractors"
with(Prestige, Boxplot(income, type, labels = rownames(Prestige)))
## [1] "general.managers" "physicians"
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")
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
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.
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.
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")
library(aplpack)
## Loading required package: tcltk
attach(mtcars)
bagplot(wt, mpg, xlab = "Car Weight", ylab = "Miles Per Gallon", main = "Bagplot Example")