統計ER

R, EZR, SPSS, KH Coder を使ったデータ分析方法を紹介するブログ。ニッチな内容が多め

ggplot でエラーバー付きグラフを書く

ggplot2というグラフをきれいに描画するパッケージを使って、エラーバー付きの平均値の折れ線グラフを描く方法を紹介する。

反復測定したエンドポイントの平均値と、SDとか95%信頼区間とかを描画する方法。

>>もう統計で悩むのを終わりにしませんか?


↑1万人以上の医療従事者が購読中

ggplot でエラーバー付きグラフを書く土台を作る

まずEZRのメニューから、反復測定分散分析を実行する。

以下のデータを修正して、グラフを描くスクリプトの前までで作成される、TempDF3というデータセットを流用する。

ggplot でエラーバー付きグラフを書く前準備をする

グラフ描画の前に、データ処理を便利にしてくれる dplyr パッケージをインストール install.packages("dplyr") して、library() で呼び出しておく。

library(dplyr)

次に、グループ FCZ のカテゴリごと、時点 time2 ごとに平均、標準偏差、n、標準誤差を集計する。

group_time_mean_sd <- TempDF3 %>% 
group_by(FCZ, time2) %>% 
summarize(mean=mean(data, na.rm=T), sd=sd(data, na.rm=T), n=n(), se=sd/sqrt(n))

ちなみに、%>%とか、group_by()とかが、dplyr パッケージの機能・関数である。

集計結果は以下の通りになる。

FCZ グループごと、time2 時点ごとに、平均、標準偏差、サンプルサイズ、標準誤差が集計・計算されている。

>>もう統計で悩むのを終わりにしませんか?


↑1万人以上の医療従事者が購読中

ggplot2を呼び出してエラーバー付きグラフを描画する

次に、ggplot2 パッケージを呼び出す。

library(ggplot2)

ggplot(), geom_line(), geom_errorbar(), geom_point() でグラフを描く。

ggplot2は、+(プラス)で関数をつなげていく、ちょっと変わった記法である。

まずは、標準偏差でエラーバーを描く場合。

ggplot(data=group_time_mean_sd, aes(x = time2, y = mean, group = FCZ, color = FCZ)) + 
  geom_line() + 
  labs(x = "Weeks", y = "CD ratio") + 
  geom_errorbar(aes(ymax = mean + sd, ymin = mean - sd), width = 0.2) + 
  geom_point(aes(color = FCZ, shape = FCZ), size = 4) 

実行すると以下のようなグラフが描ける。

エラーバーを95%信頼区間にする場合は以下のスクリプト

mean + sd のところが、mean + 1.96*se と変わっている。

ggplot(data=group_time_mean_sd, aes(x = time2, y = mean, group = FCZ, color = FCZ)) + 
  geom_line() + 
  labs(x = "Weeks", y = "CD ratio") + 
  geom_errorbar(aes(ymax = mean + 1.96*se, ymin = mean - 1.96*se), width = 0.2) + 
  geom_point(aes(color = FCZ, shape = FCZ), size = 4) 

グラフはこんな感じになる。

点とエラーバーを少しだけずらしたい場合は、position = position_dodge() を使う。

ggplot(data=group_time_mean_sd, aes(x = time2, y = mean, group = FCZ, color = FCZ)) + 
  geom_line(position = position_dodge(0.2)) + 
  labs(x = "Weeks", y = "CD ratio") + 
  geom_errorbar(aes(ymax = mean + 1.96*se, ymin = mean - 1.96*se), width = 0.2, position = position_dodge(0.2)) + 
  geom_point(aes(color = FCZ, shape = FCZ), size = 4, position = position_dodge(0.2)) 

こんな感じで少しだけずれて、見やすくなる。

学術雑誌等へ投稿する場合に、白黒で表現したければ、以下のように書く。

ggplot()の中の、aes()内のlinetype=が線を実線、破線、点線などを指定している。

theme_classic()が白黒でシンプルな見た目にする関数である。

#for academic presentation and article
ggplot(data=group_time_mean_sd, aes(x = time2, y = mean, group = FCZ, linetype = FCZ)) + 
  geom_line() + 
  labs(x = "Weeks", y = "CD ratio") + 
  geom_errorbar(aes(ymax = mean + sd, ymin = mean - sd), width = 0.2) + 
  geom_point(aes(shape = FCZ), size = 4) + 
  theme_classic()

グラフはこんな感じになる。

95%信頼区間で描画するとこんな感じ。

ggplot(data=group_time_mean_sd, aes(x = time2, y = mean, group = FCZ, linetype = FCZ)) + 
  geom_line() + 
  labs(x = "Weeks", y = "CD ratio") + 
  geom_errorbar(aes(ymax = mean + 1.96*se, ymin = mean - 1.96*se), width = 0.2) + 
  geom_point(aes(shape = FCZ), size = 4) + 
  theme_classic()

まとめ

以上、統計ソフトRのggplot2 パッケージで、きれいなエラーバー付き平均値の折れ線グラフを描く方法を紹介した。

EZRのメニューを流用して、途中からスクリプトを書くという方法で、簡単にきれいなグラフが描ける。

ぜひ、お試しあれ。

参考サイト

ggplot2を使ったグラフ作成(折れ線、時系列、エラーバー) : 干からびたウェット教授の独習でアール R

【Rテクニック】棒グラフと折れ線グラフにエラーバーを付けたい!その簡単な方法を説明します|ドクターフント

15.18 Summarizing Data with Standard Errors and Confidence Intervals | R Graphics Cookbook, 2nd edition

参考書籍