箱ひげ図の見方や引数の指定については、次を参考のこと

https://bellcurve.jp/statistics/course/5220.html

http://cse.naro.affrc.go.jp/takezawa/r-tips/r/50.html

データセットにはToothGrowth“を使う

#まずは一般的な事例として3つのグループの値に対して各々の箱ひげ図を作成する
# rnorm関数は、平均が0、標準偏差が1である正規分布に従う乱数を()内に指定した数だけ生成する。
# 箱ひげ図のひげ先は、「箱の上端(下端)+ 四分位 x 1.5」の範囲で一番大きな(小さな)データ」である
x <- rnorm(50)
y <- rnorm(50)
z <- rnorm(50)
boxplot(x)

boxplot(x, y, z)

#横方向にも描画できる
boxplot(x, y, z, horizontal= T)

#ここからデータセット ToothGrowth を使う
#ToothGrowth はどんなデータかをチェックする。
#どんな種類のオブジェクトかもチェックする。
ToothGrowth
##     len supp dose
## 1   4.2   VC  0.5
## 2  11.5   VC  0.5
## 3   7.3   VC  0.5
## 4   5.8   VC  0.5
## 5   6.4   VC  0.5
## 6  10.0   VC  0.5
## 7  11.2   VC  0.5
## 8  11.2   VC  0.5
## 9   5.2   VC  0.5
## 10  7.0   VC  0.5
## 11 16.5   VC  1.0
## 12 16.5   VC  1.0
## 13 15.2   VC  1.0
## 14 17.3   VC  1.0
## 15 22.5   VC  1.0
## 16 17.3   VC  1.0
## 17 13.6   VC  1.0
## 18 14.5   VC  1.0
## 19 18.8   VC  1.0
## 20 15.5   VC  1.0
## 21 23.6   VC  2.0
## 22 18.5   VC  2.0
## 23 33.9   VC  2.0
## 24 25.5   VC  2.0
## 25 26.4   VC  2.0
## 26 32.5   VC  2.0
## 27 26.7   VC  2.0
## 28 21.5   VC  2.0
## 29 23.3   VC  2.0
## 30 29.5   VC  2.0
## 31 15.2   OJ  0.5
## 32 21.5   OJ  0.5
## 33 17.6   OJ  0.5
## 34  9.7   OJ  0.5
## 35 14.5   OJ  0.5
## 36 10.0   OJ  0.5
## 37  8.2   OJ  0.5
## 38  9.4   OJ  0.5
## 39 16.5   OJ  0.5
## 40  9.7   OJ  0.5
## 41 19.7   OJ  1.0
## 42 23.3   OJ  1.0
## 43 23.6   OJ  1.0
## 44 26.4   OJ  1.0
## 45 20.0   OJ  1.0
## 46 25.2   OJ  1.0
## 47 25.8   OJ  1.0
## 48 21.2   OJ  1.0
## 49 14.5   OJ  1.0
## 50 27.3   OJ  1.0
## 51 25.5   OJ  2.0
## 52 26.4   OJ  2.0
## 53 22.4   OJ  2.0
## 54 24.5   OJ  2.0
## 55 24.8   OJ  2.0
## 56 30.9   OJ  2.0
## 57 26.4   OJ  2.0
## 58 27.3   OJ  2.0
## 59 29.4   OJ  2.0
## 60 23.0   OJ  2.0
?ToothGrowth
class(ToothGrowth)
## [1] "data.frame"
#?ToothGrowth  によりタイトルが "The Effect of Vitamin C on Tooth Growth in Guinea Pigs"と表示された。
#ギニアで60頭のぶたにオレンジジュースかアスコルビン酸に混ぜて、ビタミンCを投与した時の歯の成長記録のようだ。

#plot(x, y) のxの値がこの場合、数値ではなくファクタとなっているので、plot関数により箱ひげ図が自動生成される
plot(ToothGrowth$supp, ToothGrowth$len)

#ただし、箱ひげ図描画にはboxplot関数がある
boxplot(len ~ supp, data=ToothGrowth)

#x軸の2つの変数の相互関係を図示する
#x軸のsuppとdoseの組合せに対し、y軸にlen(長さ)についてグラフ化している
boxplot(len ~ supp + dose, data=ToothGrowth)

#次にggplot2パッケージを使う
library(ggplot2)   # ggplot関数、 (qplot関数)を利用するため
## Warning: package 'ggplot2' was built under R version 3.3.2

library(dplyr)     # %>%  パイプ演算子を利用するため
## Warning: package 'dplyr' was built under R version 3.3.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#次にggplot関数を使ってみる。ただしラインの色に赤を指定する
ToothGrowth %>% ggplot(aes(x=supp, y=len)) + geom_boxplot()

#interaction()を変数で組合わせることにより、複数の変数を組合せて使用した箱ひげ図を作れる。
#つまり、ここではsuppとdoseの2つの要素の組合せをx軸とし、これに対するx軸(length歯の成長)を箱ひげ図で描画する

#ggplotによる描画は次の通り。
#せっかくなので、タイトルを付けることとする。 
#labs(title=.......)のレイヤ追加による。 theme(.....)はタイトルの位置や文字の大きさ指定。
#また、分かり易いように、レイヤの追加を2つに分け、最後にfig1にて描画する。
a <- ToothGrowth %>% ggplot(aes(x=interaction(supp, dose), y=len)) + geom_boxplot() 

fig1 <- a + labs(title="The Effect of Vitamin C on Tooth Growth in Guinea Pigs") + theme(plot.title = element_text(hjust = 0.5), text = element_text(size = 15))

fig1

データセットirisを使って箱ひげ図を描画する

3種類のiris各々についてのSepal.Length を描画する

library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
a <- iris %>% ggplot(aes(x=Species, y=Sepal.Length)) + geom_boxplot() 
b <- iris %>% ggplot(aes(x=Species, y=Sepal.Width)) + geom_boxplot() 
c <- iris %>% ggplot(aes(x=Species, y=Petal.Length)) + geom_boxplot() 
d <- iris %>% ggplot(aes(x=Species, y=Petal.Width)) + geom_boxplot() 

grid.arrange(a, b, c, d, ncol=2)

本日の課題

箱ひげ図には残念ながら平均値の情報がない。

そこで平均値についての情報を、次のレイヤ追加により描画することとする。

stat_summary(fun.y=mean, geom="point", shape=3, size=4, col="red") 

ここで、shape, size, colなどの引数の値を変更することにより、どのような変化があるか確認してみるとよい。

さらに、Sepal.Lengthなどの元々の数値を点で追加することとする。

これは 次のレイヤを追加することにより可能である。

geom_jitter()  #ただし、実際の描画では次の引数指定を必ず行うこと

点の大きさはgeom_jitter( )に引数size=0.1を指定することにより、点があまり目障りにならないように描画している。

平均値および数値を点で表した次の箱ひげ図を作成し、いつものようにrmdによるhtmlファイルを作成し、moodleにアップしてください。