ggplot2で論文用の白黒折れ線グラフ
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| ggplot2で論文用の白黒折れ線グラフ [2018/08/12] – [エラーバーを片方だけにつける] adash333 | ggplot2で論文用の白黒折れ線グラフ [2020/08/12] (現在) – [ggplot2で論文用の白黒折れ線グラフ] adash333 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== ggplot2で論文用の白黒折れ線グラフ ====== | ====== ggplot2で論文用の白黒折れ線グラフ ====== | ||
| + | |||
| + | [[初めての医療統計: | ||
| ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。 | ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。 | ||
| 行 5: | 行 7: | ||
| 基本的には、< | 基本的には、< | ||
| - | ===== 1.サマリー作成用の関数の定義 | + | ===== ソースコード |
| - | ggplot2()でグラフを記載する前に、データを整理する必要があります。 | + | |
| - | Plotting means and error bars (ggplot2)\\ | + | https://colab.research.google.com/drive/1yPHODB6sgUU9OJx48tEibAF9rnUHDv-s? |
| - | http://www.cookbook-r.com/Graphs/ | + | |
| - | < | ||
| - | summarySE <- function(data=NULL, | ||
| - | conf.interval=.95, | ||
| - | library(plyr) | ||
| - | # New version of length which can handle NA's: if na.rm==T, don't count them | ||
| - | length2 <- function (x, na.rm=FALSE) { | ||
| - | if (na.rm) sum(!is.na(x)) | ||
| - | else | ||
| - | } | ||
| - | # This does the summary. For each group' | ||
| - | # N, mean, and sd | ||
| - | datac <- ddply(data, groupvars, .drop=.drop, | ||
| - | .fun = function(xx, | ||
| - | c(N = length2(xx[[col]], | ||
| - | mean = mean | ||
| - | sd = sd | ||
| - | ) | ||
| - | }, | ||
| - | measurevar | ||
| - | ) | ||
| - | # Rename the " | ||
| - | datac <- rename(datac, | ||
| - | datac$se <- datac$sd / sqrt(datac$N) | ||
| - | # Confidence interval multiplier for standard error | + | ===== 開発環境 ===== |
| - | # Calculate t-statistic for confidence interval: | + | https://rstudio.cloud/ |
| - | # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1 | + | |
| - | ciMult <- qt(conf.interval/2 + .5, datac$N-1) | + | |
| - | datac$ci <- datac$se * ciMult | + | |
| - | return(datac) | + | 2018年8月現在、メールアドレスを登録するのみで、無料です。 |
| - | } | + | |
| - | </ | + | |
| - | ===== 2.白黒の折れ線グラフの作成 ===== | + | [[https:// |
| + | |||
| + | {{: | ||
| + | |||
| + | インストール終了後の画面 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | 参考:http:// | ||
| + | |||
| + | ===== Rmdファイルの新規作成と保存 ===== | ||
| + | File > New File > R Notebook で、R Notebookファイルを新規作成。 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | 以下のようにR Notebookファイルが作成されるので、 | ||
| + | |||
| + | |||
| + | |||
| + | ===== 1.グラフ作成用に平均と標準偏差を計算 | ||
| + | ggplot2()でグラフを記載する前に、データを整理する必要があります。 | ||
| + | |||
| + | dplyr(tidyverse)の | ||
| + | -<wrap hi> | ||
| + | -<wrap hi> | ||
| + | を用いて、グループごとに、個数、平均、標準偏差を計算して新しい表を作成します。 | ||
| < | < | ||
| library(tidyverse) | library(tidyverse) | ||
| - | tg <- ToothGrowth | + | head(ToothGrowth) |
| - | tgc <- summarySE(tg, measurevar="len", groupvars=c(" | + | str(ToothGrowth) |
| + | |||
| + | d <- ToothGrowth %>% | ||
| + | group_by(supp, dose) %>% | ||
| + | summarise( | ||
| + | n=n(), | ||
| + | mean_len=mean(len), | ||
| + | sd_len=sd(len) | ||
| + | ) | ||
| + | # 蛇足だが、以下の書き方はsdがNAになってしまうので注意 | ||
| + | # https:// | ||
| + | # summarise(n=n(), len=mean(len), sd=sd(len)) | ||
| + | d | ||
| pd <- position_dodge(0.1) # move them .05 to the left and right | pd <- position_dodge(0.1) # move them .05 to the left and right | ||
| - | ggplot(tgc, aes(x=dose, y=len, shape=supp)) + | + | ggplot(d, aes(x=dose, y=mean_len, shape=supp)) + |
| theme_set(theme_classic()) + | theme_set(theme_classic()) + | ||
| geom_point(size=4, | geom_point(size=4, | ||
| - | geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1, position=pd) + | + | geom_errorbar(aes(ymin=mean_len-sd_len, ymax=mean_len+sd_len), width=.1, position=pd) + |
| geom_line(aes(linetype = supp), position=pd) | geom_line(aes(linetype = supp), position=pd) | ||
| + | </ | ||
| + | ===== 2.白黒の折れ線グラフの作成 ===== | ||
| + | |||
| + | 上記で作成した表dを用いて、ggplot()関数でグラフを描きます。 | ||
| + | |||
| + | < | ||
| + | pd <- position_dodge(0.1) # move them .05 to the left and right | ||
| + | |||
| + | ggplot(d, aes(x=dose, y=mean_len, shape=supp)) + | ||
| + | theme_set(theme_classic()) + | ||
| + | geom_point(size=4, | ||
| + | geom_errorbar(aes(ymin=mean_len-sd_len, | ||
| + | geom_line(aes(linetype = supp), position=pd) + | ||
| + | # グラフにタイトルをつける | ||
| + | ggtitle(" | ||
| + | theme(plot.title = element_text(hjust=0.5)) + | ||
| + | theme(plot.title = element_text(size = 20), | ||
| + | axis.title.x = element_text(size= 20), | ||
| + | axis.title.y = element_text(size= 20), | ||
| + | axis.text.x = element_text(size= 20), | ||
| + | axis.text.y = element_text(size= 20) | ||
| + | ) + | ||
| + | # y軸のラベルの書き換え | ||
| + | labs(y=" | ||
| </ | </ | ||
| 以下のようなグラフが出力されます。 | 以下のようなグラフが出力されます。 | ||
| - | {{:pasted: | + | {{ :ダウンロード.png |}} |
| ===== 3.グラフをpngファイルで出力 ===== | ===== 3.グラフをpngファイルで出力 ===== | ||
| 行 139: | 行 175: | ||
| https:// | https:// | ||
| + | |||
| + | ===== グラフのtitleを真ん中上に記載 ===== | ||
| + | |||
| + | |||
| + | |||
| + | < | ||
| + | library(tidyverse) | ||
| + | |||
| + | tg <- ToothGrowth | ||
| + | tgc <- summarySE(tg, | ||
| + | |||
| + | ggplot(tgc, aes(x=dose, y=len, shape=supp)) + | ||
| + | theme_set(theme_classic()) + | ||
| + | |||
| + | ylim(0, 40) + | ||
| + | | ||
| + | ggtitle(" | ||
| + | theme(plot.title = element_text(hjust = 0.5)) | ||
| + | |||
| + | </ | ||
| + | ===== リンク ===== | ||
| + | |||
| + | |||
| + | |||
| + | https:// | ||
| 行 158: | 行 219: | ||
| https:// | https:// | ||
| + | ===== リンク ===== | ||
| + | 目次:[[初めての医療統計: | ||
| + | |||
| + | 前: | ||
| + | |||
| + | 次: | ||
ggplot2で論文用の白黒折れ線グラフ.1534046515.txt.gz · 最終更新: 2018/10/07 (外部編集)
