ggplot2で論文用の白黒折れ線グラフ
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン次のリビジョン両方とも次のリビジョン | ||
ggplot2で論文用の白黒折れ線グラフ [2018/08/10] – [リンク] adash333 | ggplot2で論文用の白黒折れ線グラフ [2020/08/12] – [2.白黒の折れ線グラフの作成] adash333 | ||
---|---|---|---|
行 5: | 行 5: | ||
基本的には、< | 基本的には、< | ||
- | ===== 見出し | + | ===== ソースコード |
- | 作成中 | + | |
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== 開発環境 ===== | ||
+ | https:// | ||
+ | |||
+ | 2018年8月現在、メールアドレスを登録するのみで、無料です。 | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | {{: | ||
+ | |||
+ | インストール終了後の画面 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 参考:http:// | ||
+ | |||
+ | ===== Rmdファイルの新規作成と保存 ===== | ||
+ | File > New File > R Notebook で、R Notebookファイルを新規作成。 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 以下のようにR Notebookファイルが作成されるので、 | ||
+ | |||
+ | |||
+ | |||
+ | ===== 1.グラフ作成用に平均と標準偏差を計算 ===== | ||
+ | ggplot2()でグラフを記載する前に、データを整理する必要があります。 | ||
+ | |||
+ | dplyr(tidyverse)の | ||
+ | -<wrap hi> | ||
+ | -<wrap hi> | ||
+ | を用いて、グループごとに、個数、平均、標準偏差を計算して新しい表を作成します。 | ||
+ | |||
+ | < | ||
+ | library(tidyverse) | ||
+ | |||
+ | head(ToothGrowth) | ||
+ | str(ToothGrowth) | ||
+ | |||
+ | d <- ToothGrowth %>% | ||
+ | group_by(supp, | ||
+ | summarise( | ||
+ | n=n(), | ||
+ | mean_len=mean(len), | ||
+ | sd_len=sd(len) | ||
+ | ) | ||
+ | # 蛇足だが、以下の書き方はsdがNAになってしまうので注意 | ||
+ | # https:// | ||
+ | # summarise(n=n(), | ||
+ | d | ||
+ | |||
+ | 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) | ||
+ | </ | ||
+ | |||
+ | ===== 2.白黒の折れ線グラフの作成 ===== | ||
+ | |||
+ | < | ||
+ | |||
+ | </ | ||
+ | |||
+ | 以下のようなグラフが出力されます。 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== 3.グラフをpngファイルで出力 ===== | ||
+ | |||
+ | 以下のように、ggsave()関数で、グラフをpngファイルで出力することができます。 | ||
+ | |||
+ | グラフを出力する方法としては、RmdファイルをKnit(htmlファイルなどに変換すること)してから、htmlファイル上の画像を右クリックして保存することでも、保存することができます。 | ||
+ | |||
+ | < | ||
+ | library(tidyverse) | ||
+ | |||
+ | p0 = ggplot(mpg, aes(x = displ, y = cty)) | ||
+ | p1 = p0 + geom_point() | ||
+ | p2 = p1 + theme_classic(base_size = 20, base_family = " | ||
+ | p3 = p2 + stat_smooth(method = lm, formula = y ~ log(x)) | ||
+ | |||
+ | ggsave(" | ||
+ | </ | ||
+ | Rmdファイルと同じフォルダに、以下のようなgraph.pngが保存されます。 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== エラーバーを片方だけにつける ===== | ||
+ | 下記リンク先によりますと、 | ||
+ | < | ||
+ | 1. geom_errorbar()でwidth=0のエラーバーを作成 | ||
+ | 2. geom_segment()で片方だけエラーバーの横線を追加 | ||
+ | </ | ||
+ | という流れになります。 | ||
+ | |||
+ | geom_segment()でエラーバーのxの始点と終点を指定するときに、 | ||
+ | < | ||
+ | 横軸が数字であれば、as.numeric(key)-0.1のように指定する | ||
+ | |||
+ | 横軸が文字(00pre, | ||
+ | keyを0, | ||
+ | as.numeric()で数値に変更しておく。 | ||
+ | </ | ||
+ | |||
+ | ところが少し難しいポイントとなります。 | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | pd <- position_dodge(0.1) # move them .05 to the left and right | ||
+ | |||
+ | ggplot(xc, aes(x=key, y=value, shape=DM, group = DM)) + | ||
+ | theme_set(theme_classic()) + | ||
+ | geom_point(size=4) + | ||
+ | #errorbar without caps | ||
+ | geom_errorbar(data=with(xc, | ||
+ | aes(ymin = value, ymax = value+se), | ||
+ | geom_errorbar(data=with(xc, | ||
+ | aes(ymin = value-se, ymax = value), | ||
+ | geom_line(aes(linetype = DM), position=pd) + | ||
+ | # | ||
+ | geom_segment(data=with(xc, | ||
+ | aes(y=value+se, | ||
+ | geom_segment(data=with(xc, | ||
+ | aes(y=value-se, | ||
+ | ylim(230, 300) + | ||
+ | ggtitle(" | ||
+ | theme(plot.title = element_text(hjust = 0.5)) | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | 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:// | ||
+ | |||
+ | |||
+ | |||
+ | ===== リンク(英語) ===== | ||
https:// | https:// | ||
+ | Plotting means and error bars (ggplot2)\\ | ||
+ | http:// | ||
行 17: | 行 192: | ||
Line plot for two-way designs using ggplot2\\ | Line plot for two-way designs using ggplot2\\ | ||
https:// | https:// | ||
+ | |||
+ | |||
+ | Chapter 4. Line Graphs\\ | ||
+ | https:// | ||
+ | |||
+ | ===== リンク ===== | ||
+ | 目次:[[初めての医療統計: | ||
+ | |||
+ | 前: | ||
+ | |||
+ | 次: | ||
+ | |||
ggplot2で論文用の白黒折れ線グラフ.txt · 最終更新: 2020/08/12 by adash333