説明
与えられた(グループ化された)データから、箱ひげ図(box-and-whisker plot(s))を作成する。
使用法
# 基本構文
boxplot(x, ...)
# S3 method for class 'formula':
boxplot(formula, data = NULL, ..., subset, na.action = NULL)
# Default S3 method:
boxplot(x, ..., range = 1.5, width = NULL, varwidth = FALSE, notch = FALSE, outline = TRUE, names, plot = TRUE, border = par("fg"), col = NULL, log = "", pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5), horizontal = FALSE, add = FALSE, at = NULL)引数
formula:y ~ grpのようなformulaオブジェクト。ここでyは数値型ベクトルであり、変数grp(通常因子型)で指定されるグルーピング変数によりグループごとに分割される。
data:formulaで使用する変数の取得元となるデータフレームあるいはリスト、subset:プロットに使用するデータ(observation)を指定するために使用するベクトル。省略可能である。
na.action:データに欠損値(NAs)がある場合の処理方法を指定する関数。デフォルトは欠損値を無視する(応答変数であれグループ変数であれ)。
x:箱ひげ図を描画する元データを取得するデータを指定する。数値型ベクトルあるいは数値型ベクトルを含む単一リスト。追加の無名引数は追加のデータを別のベクトル(それぞれがコンポーネントの箱ひげ図に対応する)を指定する。データ内の欠損値(NAs)は許容される。
…:formulaメソッドの場合、デフォルトメソッドに渡す名前付き引数。デフォルトメソッドの場合、無名引数は追加のデータベクトルである(xがリストでない限りそれらは無視される)、名前付き引数は引数pars(pars内の引数を上書きする)に加えてbxpに渡されれる引数およびグラフィカルパラメータである。bxpはグラフィカルパラメータを使用する場合も使用しない場合もあることに注意せよ。
range: ヒゲが箱からどのくらい離れたところまで延ばされるかを決める。rangeが正の数の場合、ヒゲは箱から1/4範囲のrange倍を上限として、最も外れたデータまで延ばされる。値がゼロの場合最も外れたデータまでヒゲが延ばされる。
width: プロットを構成する箱の相対的な幅を指定するベクトル。
varwidth: 値がTRUEの場合、箱の幅は当該グループのレコード数の平方根に比例する幅で描画される。
notch: 値がTRUEの場合、箱の両側にくびれ(notch)が描画される。2つのプロットのノッチがオーバラップしない場合、2群の中央値が異なることの強い証拠となる (Chambers et al, 1983, p. 62)。使用される計算に関してはboxplot.stats()を参照せよ。
outline: 値がTRUEでない場合、外れ値は描画されない(点として描画されない、一方S+は線を使用する一方)。
names: 各箱ひげ図の下に描画されるグループラベル。文字ベクトルあるいは式(expression)を使用可能。plotmathを参照せよ。
boxwex: 全ての箱に適用されるスケール因子である。2,3のグループしかない場合、箱の幅を狭くすることでプロットの見た目を改善できる。
staplewex: 箱の幅に比例する、拡張を伴うステープル線(staple line width expansion)。
outwex: 箱の幅に比例する、拡張を伴う外れ値の線(outlier line width expansion)。plot 値がTRUE(デフォルト)の場合、箱ひげ図が描画される。値がFALSEの場合、箱ひげ図の下になる要約値が返される。
border:箱ひげ図の外れ値に対する色を指定する省略可能なベクトル。border引数の値はリサイクルされる(border引数の長さが描画される点の数より短い場合)。
col: colが非nullの場合、箱の本体を描画するために使用される色を指定していると解釈される。デフォルトでは背景色である。
log: x軸、y軸のいずれかあるいは両方が対数スケールで描画されるかどうかを指定する文字(x,y,bothのいずれか)。
pars: 更なるグラフィカルパラメータのリスト。例えば boxwexやoutpch。これらはbxpに渡される (plot引数がTRUEの場合); 詳細はbxpを見よ。
horizontal:論理値。TRUEなら箱ひげ図は水平方向に並ぶ。デフォルトはFALSEであり、箱ひげ図は垂直方向に並ぶ。add 論理値。TRUEなら箱ひげ図を現在のプロットに追加する。
at:箱ひげ図が描画される位置を指定する数値型ベクトル、特にadd=TRUEの場合。デフォルトは 1:n (nは箱の数)
以下は未訳出である。
詳細
The generic function boxplot currently has a default method (boxplot.default) and a formula interface (boxplot.formula).
If multiple groups are supplied either as multiple arguments or via a formula, parallel boxplots will be plotted, in the order of the arguments or the order of the levels of the factor (see factor).
Missing values are ignored when forming boxplots.
返り値
以下の要素からなるリストが返される。
stats a matrix, each column contains the extreme of the lower whisker, the lower hinge, the median, the upper hinge and the extreme of the upper whisker for one group/plot. If all the inputs have the same class attribute, so will this component.
n a vector with the number of observations in each group.
conf a matrix where each column contains the lower and upper extremes of the notch.
out the values of any data points which lie beyond the extremes of the whiskers.
group a vector of the same length as out whose elements indicate to which group the outlier belongs.
names a vector of names for the groups.
参考文献
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Chambers, J. M., Cleveland, W. S., Kleiner, B. and Tukey, P. A. (1983) Graphical Methods for Data Analysis. Wadsworth & Brooks/Cole.
Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.
See also boxplot.stats.
See Also
boxplot.stats which does the computation, bxp for the plotting and more examples; and stripchart for an alternative (with small data sets).
使用例
boxplot on a formula:
boxplot(count ~ spray, data = InsectSprays, col = “lightgray”)# *add* notches (somewhat funny here):boxplot(count ~ spray, data = InsectSprays, notch = TRUE, add = TRUE, col = “blue”)
boxplot(decrease ~ treatment, data = OrchardSprays, log = “y”, col = “bisque”)
rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col = “bisque”)title(“Comparing boxplot()s and non-robust mean +/- SD”)
mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)xi <- 0.3 + seq(rb$n)points(xi, mn.t, col = “orange”, pch = 18)arrows(xi, mn.t – sd.t, xi, mn.t + sd.t, code = 3, col = “pink”, angle = 75, length = .1)
## boxplot on a matrix:
mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100), `5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))boxplot(as.data.frame(mat), main = “boxplot(as.data.frame(mat), main = …)”)par(las = 1) # all axis labels horizontalboxplot(as.data.frame(mat), main = “boxplot(*, horizontal = TRUE)”, horizontal = TRUE)
## Using ‘at = ‘ and adding boxplots — example idea by Roger Bivand :
boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 – 0.2, subset = supp == “VC”, col = “yellow”, main = “Guinea Pigs’ Tooth Growth”, xlab = “Vitamin C dose mg”, ylab = “tooth length”, xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = “i”)boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == “OJ”, col = “orange”)legend(2, 9, c(“Ascorbic acid”, “Orange juice”), fill = c(“yellow”, “orange”))
more examples in help(bxp)


コメント