{{category R}} R.package !!!ggplot2 {{outline}} ---- !!基本:グラフの部品を重ねていく +ggplot(データを指定) +aes でグラフの要素の指定(横軸は何、縦軸は何) # aestheticより +geom_何とかでグラフを書く ++geom_point で散布図 ++geom_smooth で回帰線 *theme で見た目の設定(なくてもよい) !最低限これだけ *一種類のデータの分布 ggplot(data, aes(x = カラム名)) + geom_histogram() **x軸とy軸に注意:boxplotはy軸しかないので ggplot(data, aes(y = カラム名)) + geom_boxplot() !タイトルとx軸とy軸のラベル ggtitle("タイトルを書きます") + xlab("x軸のラベル") + ylab("y軸のラベル") + !!データの形式はlong-format *tidyverseのパッケージのセットに入っているtidyrというパッケージの中のpivot_longer()を使って変換 !サンプルデータ {{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 }} *もしくは、gather() !!ヒストグラム > 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}} !重なって見にくいので、透明度を設定:alpha=0.7 {{pre ggplot(cn2cn3jpyTID, aes(x=SL, fill=year)) + geom_density(alpha=0.7) }} {{ref_image SL.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_smooth(method = "lm", とすると直線 **デフォルトでは、近似曲線(一般化加法モデルの回帰線) *geom_histogram() ヒストグラム *geom_line() 折れ線グラフ *geom_violin() バイオリンプロット *geom_boxplot() 箱ひげ図 *geom_bar() 棒グラフ *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}} !!複数のグラフを配置する {{pre library("gridExtra") grid.arrange(一つ目の図, 二つ目の図, ncol=2) }} !!折れ線グラフを重ねる {{pre ggplot(final.long.data, aes(x=students, y=value, color=category, group=category)) + geom_line() }} !!棒グラフを並べる {{pre ggplot(final.long.data, aes(x=category, y=value, fill=students)) + geom_bar(stat="identity", position="dodge") }} !!boxplot !!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") !軸を入れ替える: coord_flip() !!中でデータ処理: stat_関数 https://qiita.com/swathci/items/b08496d863bca4b479b3 !!References *https://qiita.com/roadricefield/items/40e92b3d78eaa5e84190 *https://stats.biopapyrus.jp/r/ggplot/ *https://www.jaysong.net/ggplot_intro2/ *https://ggplot2-book.org/ *https://heavywatal.github.io/rstats/ggplot2.html