Mukku John Blog

取り組んでいること を つらつら と

ggplot2を使って、データ分布を要約する-3

R グラフィックス クックブック 17回目

ggplot2パッケージを利用して、データ分布を可視化していきます。

利用するデータ

R: Risk Factors Associated with Low Infant Birth Weightデータセットを使います。
乳幼児の出生時の体重に関わるリスク要因のデータです。

> str(birthwt)
'data.frame':	189 obs. of  10 variables:
 $ low  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
 $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
 $ race : int  2 3 1 1 1 3 1 3 1 1 ...
 $ smoke: int  0 0 1 1 1 0 0 0 1 1 ...
 $ ptl  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ht   : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ui   : int  1 0 0 1 1 0 0 0 0 0 ...
 $ ftv  : int  0 3 1 2 0 0 1 1 1 0 ...
 $ bwt  : int  2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 ...

> head(birthwt)
   low age lwt race smoke ptl ht ui ftv  bwt
85   0  19 182    2     0   0  0  1   0 2523
86   0  33 155    3     0   0  0  0   3 2551
87   0  20 105    1     1   0  0  0   1 2557
88   0  21 108    1     1   0  0  1   2 2594
89   0  18 107    1     1   0  0  1   0 2600
91   0  21 124    3     0   0  0  0   0 2622

race値を使いますので、ファクタに変換して、ラベル付けします。

birthwt2 <- birthwt
birthwt2$race <- factor(birthwt2$race)
birthwt2$race <- revalue(birthwt2$race,c("1"="white","2"="black","3"="other"))

箱ひげ図

箱ひげ図 - Wikipedia *1
箱ひげ図を作成するには、geom_boxplot()を使います。
x軸に離散値を、y軸に連続値をマッピングします。

ggplot(birthwt2, aes(x=race, y=bwt)) + 
  geom_boxplot()

f:id:MukkuJohn:20160918094131p:plain

他のグラフと同様に、枠線:colour/塗りつぶし:fillで変更できます。

ggplot(birthwt2, aes(x=race, y=bwt)) + 
  geom_boxplot(colour="grey60",fill="cornsilk") +
  theme_bw()

f:id:MukkuJohn:20160918094850p:plain


箱の幅を変更するためには、widthを指定します。

ggplot(birthwt2, aes(x=race, y=bwt)) + 
  geom_boxplot(width=.5)

f:id:MukkuJohn:20160918095022p:plain


外れ値のマークは、大きさと形を指定できます。
outlierを指定します。

ggplot(birthwt2, aes(x=race, y=bwt)) + 
  geom_boxplot(width=.5,
               outlier.size = 4,
               outlier.shape = 21,
               outlier.colour = "red")

f:id:MukkuJohn:20160918095304p:plain


ノッチ*2を追加するには、notchを指定します。

#notchのデフォルト値はFALSE
ggplot(birthwt2, aes(x=race,y=bwt)) +
  geom_boxplot(notch = TRUE, colour="grey60",fill="cornsilk") +
  theme_bw()

f:id:MukkuJohn:20160918100130p:plain


平均値を追加するためには、stat_summary()を追加します。

ggplot(birthwt2, aes(x=race,y=bwt)) +
  geom_boxplot(notch = TRUE, colour="grey60",fill="cornsilk") +
  stat_summary(fun.y="mean",geom="point") +
  theme_bw()

f:id:MukkuJohn:20160918100429p:plain

平均値を表すマークは、大きさ、形、色/塗りつぶしが指定できます。

ggplot(birthwt2, aes(x=race,y=bwt)) +
  geom_boxplot(notch = TRUE, colour="grey60",fill="cornsilk") +
  stat_summary(fun.y="mean",geom="point", size=3, shape=22, fill="lightblue",colour="blue") +
  theme_bw()

f:id:MukkuJohn:20160918100736p:plain

バイオリンプロット

密度曲線*3を左右対称でプロットしたモノです。

この密度曲線をグリッドで分けることなく、確認できます。
f:id:MukkuJohn:20160918103117p:plain

バイオリンプロットを使うためには、geom_violin()を使います。

ggplot(birthwt2,aes(x=race,y=bwt)) +
  geom_violin() 

f:id:MukkuJohn:20160918101845p:plain


線の色や、塗りつぶし、透明度は他のグラフと同じ指定です。

ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60",fill="blue",alpha=.2) +
  theme_bw()

f:id:MukkuJohn:20160918102104p:plain

上下端が切れてしまっています。
デフォルトでは、最大値/最小値で、平らにカットされてしまいます。
平らにカットしない場合は、trimを指定します。

ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60", fill="blue", alpha=.2,
              trim=FALSE) +
  theme_bw()

f:id:MukkuJohn:20160918113053p:plain


バイオリンプロットの塗りつぶされている面積ですが、
デフォルトでは、各バイオリンの面積が等しくになっています。

面積を観測値の数と等しくするためには、scaleを指定します。

ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60", fill="blue", alpha=.2,
              trim=FALSE,
              scale="count") +
  theme_bw()

f:id:MukkuJohn:20160918113342p:plain


繰り返しになりますが、
バイオリンプロットは、密度曲線を左右対称にプロットしたモノですので、
密度曲線と同じように、平滑化できます。

平滑化の指定は、密度曲線と同じ、adjustを指定します。

ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60", fill="blue", alpha=.2,
              trim=FALSE,
              scale="count",
              adjust=2) +
  theme_bw()

f:id:MukkuJohn:20160918113556p:plain

ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60", fill="blue", alpha=.2,
              trim=FALSE,
              scale="count",
              adjust=.5) +
  theme_bw()

f:id:MukkuJohn:20160918113653p:plain


ヒストグラム-密度曲線と同様に、箱ひげ図-バイオプロットと重ねることで
観測値の分布(=箱ひげ図)推定値の分布(=バイオリンプロット)をまとめます。

#バイオリンプロット → 箱ひげ図 → 中央値マーク
ggplot(birthwt2,aes(x=race,y=bwt))+
  geom_violin(colour="grey60", fill="blue", alpha=.2,
              trim=FALSE,
              scale="count") +
  geom_boxplot(width=.2,fill="cornsilk",outlier.colour = NA) +
  stat_summary(fun.y=median,geom="point",fill="green",
               shape=21,size=3) +
  theme_bw()

f:id:MukkuJohn:20160918114000p:plain


次回は、データポイントを表示するドットプロットを作成します。

*1:箱ひげ図の読み方は、こちら mukkujohn.hatenablog.com

*2:中央値の95%信頼区間

*3:密度曲線については、この記事。mukkujohn.hatenablog.com