{{category R}} R.package !!!ggplot2 {{outline}} *Reference https://ggplot2-book.org/ https://heavywatal.github.io/rstats/ggplot2.html !!基本:グラフの部品を重ねていく *ggplot(データ) **aes でグラフの要素の指定 # aestheticより *geom_point で散布図 *geom_smooth で回帰線 *theme で見た目の設定(なくてもよい) !最低限これだけ *一種類のデータの分布 ggplot(data, aes(x = カラム名)) + geom_histogram() **x軸とy軸に注意:boxplotはy軸しかないので ggplot(data, aes(y = カラム名)) + geom_boxplot() !!サンプルデータ {{pre > head(onomato.data2) subj cond timing score 1 1 exp pre 65 2 2 exp pre 75 3 3 exp pre 35 4 4 exp pre 70 5 5 exp pre 85 6 6 exp pre 55 }} !!ヒストグラム > ggplot(onomato.data2, aes(x=score)) + geom_histogram() {{ref_image histo1.png}} !カーネル密度推定 > ggplot(onomato.data2, aes(x=score)) + geom_density() {{ref_image density1.png}} *データのカテゴリーによって表示を分けて塗りつぶす fill = 条件 オプション > ggplot(onomato.data2, aes(x=score, fill = cond)) + geom_density() **コントロール群と統制群の条件で分けてみる {{ref_image cond1.png}} > ggplot(onomato.data2, aes(x=score, fill = timing)) + geom_density() **事前・事後・遅延のタイミングで分けてみる {{ref_image timing1.png}} !二種類・二次元のデータの分布(散布図) ggplot(data, aes(x, y)) + geom_point() + geom_smooth() *回帰直線にしたいときは geom_smooth(<>) *細かい使い方は、 ?geom_smooth のようにして調べる。 {{pre > ggplot(shadow.tidy2, aes(x=Score, y=Ctest)) + geom_point() + geom_smooth() + theme_bw() > }} {{ref_image Score-Ctest.png}} !!何を描くかを決める geom_ *geom_point() 散布図 *geom_smooth() 近似直(曲)線 *geom_histogram() ヒストグラム *geom_line() 折れ線グラフ *geom_violin() バイオリンプロット *geom_boxplot() 箱ひげ図 *geom_density() <<カーネル密度推定>>で密度プロットを描写 !!基本のスタイルを決める theme_ *theme_classic() クラシック:枠なし格子なし *theme_bw() 白黒:黒枠灰色格子 *theme_grey() 灰色背景:いかにもggplot2 *theme_linedraw() はっきりした格子 *theme_light() 薄い格子 !!積み重ねの各段階を保存して、そこまでの出力をする *こうしておくと、各段階で、修正をしやすい。 {{pre step1 <- ggplot(data, aes()) step2 <- step1 + geom_point() step3 <- step2 + geom_smooth() step4 <- step3 + theme_classic() print(step4) }} {{pre > step1 <- ggplot(shadow.tidy2, aes(x=Score, y=Ctest)) > step2 <- step1 + geom_point() > step3 <- step2 + geom_smooth(method="lm") > step4 <- step3 + theme_linedraw() > print(step4) }} {{ref_image Score-Ctest-lm.png}} !!複数の種類のデータを重ねる *クラスごととか *個人ごとに複数回のデータとか *IDカラムに種類わけの番号を振っておく(tidydata形式) *一人が8週間エッセイを書いた例(pidで並べ替えて、weekで並べ替えた) *<<データが整数になっていることを確認>> なってない場合は、as.integer() {{pre pid week score 8 1 1 1 4 1 2 1 3 1 3 2 6 1 4 3 7 1 5 3 2 1 6 3 5 1 7 3 1 1 8 3 9 2 1 2 12 2 2 3 }} ggplot(nicest.sample, aes(week, score, group = pid)) + geom_point() + geom_smooth(method = "lm", se = F) *一行目、group = pid に注意 **pidの番号でグループ化して、そのまとまりごとにグラフを描く *三行目、se = F に注意 **信頼区間の表示をFALSEに {{ref_image nicest.sample.png}} *線が多すぎて、わかりにくいので、色を指定 aes()に color = 見出し オプションで指定。 {{pre ggplot(nicest.sample, aes(week, score, group = pid, color = pid)) + geom_point() + geom_smooth(method = "lm", se = F) }} {{ref_image nicest.sample.color.png}} !複数のグラフを別々に表示 <> オプションの追加 * <<(~pid)>> に注意 ** pid 毎に一枚ずつにするという意味 ggplot(nicest.sample, aes(week, score, group = pid)) + geom_point() + geom_smooth(method = "lm", se = F) + facet_wrap(~pid) {{ref_image nicest.sample2.png}} !!Tips !エラーバーをつける *平均と標準誤差を事前に出しておく **標準誤差=標準偏差/sqrt(サンプルサイズ) *平均からプラスマイナス標準誤差を描く geom_errorbar(aes(ymin = score.mean - score.se, ymax = score.mean + score.se, width = 0.3)) **widthは横の棒の長さ !y軸の範囲を設定する ylim(c(1, 6)) !y軸のラベルを設定する ylab("Criterion Score")