R boxplot() 関数 - 箱ひげ図を描画する
R boxplot() 関数は箱ひげ図を描画し、データの分布特性と外れ値を表示します。
箱ひげ図はデータの5数要約(最小値、Q1、中央値、Q3、最大値)と外れ値を表示します。
boxplot() 関数の構文は次のとおりです:
boxplot(x, horizontal = FALSE, col = NULL,
main = "", xlab = "", ylab = "")
パラメータの説明:
x数値ベクトルまたは式(例:数値 ~ グループ)。
例
# 複数のクラスの成績データ
class_a <- c(78, 82, 85, 88, 90, 75, 80, 86, 92, 84)
class_b <- c(72, 75, 78, 76, 80, 70, 74, 77, 79, 73)
class_c <- c(85, 88, 90, 92, 95, 80, 87, 89, 91, 86)
# 箱ひげ図を描画
boxplot(class_a, class_b, class_c,
names = c("A組", "B組", "C組"),
main = "各クラス成績分布の比較",
xlab = "クラス",
ylab = "成績",
col = c("skyblue", "lightgreen", "coral"))
class_a <- c(78, 82, 85, 88, 90, 75, 80, 86, 92, 84)
class_b <- c(72, 75, 78, 76, 80, 70, 74, 77, 79, 73)
class_c <- c(85, 88, 90, 92, 95, 80, 87, 89, 91, 86)
# 箱ひげ図を描画
boxplot(class_a, class_b, class_c,
names = c("A組", "B組", "C組"),
main = "各クラス成績分布の比較",
xlab = "クラス",
ylab = "成績",
col = c("skyblue", "lightgreen", "coral"))
上記のコードを実行すると、3つのクラスの成績の箱ひげ図比較が表示されます。
boxplot() は式形式を使用するとグループ化データにより適しています:
例
# 式形式を使用
scores <- c(class_a, class_b, class_c)
groups <- rep(c("A組", "B組", "C組"), each = 10)
boxplot(scores ~ groups,
main = "各クラス成績の箱ひげ図",
col = rainbow(3))
scores <- c(class_a, class_b, class_c)
groups <- rep(c("A組", "B組", "C組"), each = 10)
boxplot(scores ~ groups,
main = "各クラス成績の箱ひげ図",
col = rainbow(3))
R 言語の例