この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
ggplot2で論文用の白黒折れ線グラフ [2018/08/11] adash333 [見出し] |
ggplot2で論文用の白黒折れ線グラフ [2018/10/07] (現在) |
||
---|---|---|---|
ライン 4: | ライン 4: | ||
基本的には、<wrap em>ggplot()+theme_set(theme_classic())</wrap>を用いるのですが、具体的な方法を記載させていただきたいと思います。 | 基本的には、<wrap em>ggplot()+theme_set(theme_classic())</wrap>を用いるのですが、具体的な方法を記載させていただきたいと思います。 | ||
+ | |||
+ | ===== 開発環境 ===== | ||
+ | https://rstudio.cloud/ | ||
+ | |||
+ | 2018年8月現在、メールアドレスを登録するのみで、無料です。 | ||
+ | |||
+ | [[https://rstudio.cloud/|RStudio Cloud]]のconsole画面(画面左下)で、<wrap hi><wrap em>install.packages("tidyverse")</wrap></wrap>を入力して、tidyverseパッケージをインストールした状態とします。 | ||
+ | |||
+ | {{:pasted:20180813-231913.png}} | ||
+ | |||
+ | インストール終了後の画面 | ||
+ | |||
+ | {{:pasted:20180813-232101.png}} | ||
+ | |||
+ | 参考:http://twosquirrel.mints.ne.jp/?p=26918 | ||
+ | |||
+ | ===== Rmdファイルの新規作成と保存 ===== | ||
+ | File > New File > R Notebook で、R Notebookファイルを新規作成。 | ||
+ | |||
+ | {{:pasted:20180813-232234.png}} | ||
+ | |||
+ | {{:pasted:20180813-232303.png}} | ||
+ | |||
+ | 以下のようにR Notebookファイルが作成されるので、 | ||
+ | |||
+ | |||
===== 1.サマリー作成用の関数の定義 ===== | ===== 1.サマリー作成用の関数の定義 ===== | ||
ライン 49: | ライン 75: | ||
</code> | </code> | ||
- | 白黒の折れ線グラフの作成 | + | ===== 2.白黒の折れ線グラフの作成 ===== |
<code> | <code> | ||
ライン 71: | ライン 97: | ||
{{:pasted:20180811-054428.png}} | {{:pasted:20180811-054428.png}} | ||
+ | ===== 3.グラフをpngファイルで出力 ===== | ||
+ | |||
+ | 以下のように、ggsave()関数で、グラフをpngファイルで出力することができます。 | ||
+ | |||
+ | グラフを出力する方法としては、RmdファイルをKnit(htmlファイルなどに変換すること)してから、htmlファイル上の画像を右クリックして保存することでも、保存することができます。 | ||
- | グラフをpngファイルで出力することもできます。 | ||
<code> | <code> | ||
library(tidyverse) | library(tidyverse) | ||
ライン 88: | ライン 118: | ||
===== エラーバーを片方だけにつける ===== | ===== エラーバーを片方だけにつける ===== | ||
+ | 下記リンク先によりますと、 | ||
+ | <code> | ||
+ | 1. geom_errorbar()でwidth=0のエラーバーを作成 | ||
+ | 2. geom_segment()で片方だけエラーバーの横線を追加 | ||
+ | </code> | ||
+ | という流れになります。 | ||
+ | |||
+ | geom_segment()でエラーバーのxの始点と終点を指定するときに、 | ||
+ | <code> | ||
+ | 横軸が数字であれば、as.numeric(key)-0.1のように指定する | ||
+ | |||
+ | 横軸が文字(00pre, 1month, 2month, 3monthなど)であれば、 | ||
+ | keyを0,1,2,3などに置き換えてから(csvファイルを修正しておいてもよいかもしれません)、 | ||
+ | as.numeric()で数値に変更しておく。 | ||
+ | </code> | ||
+ | |||
+ | ところが少し難しいポイントとなります。 | ||
+ | |||
+ | |||
+ | <code> | ||
+ | |||
+ | 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,xc[which(DM=='D'),]), | ||
+ | aes(ymin = value, ymax = value+se),width=0) + | ||
+ | geom_errorbar(data=with(xc,xc[which(DM=='N'),]), | ||
+ | aes(ymin = value-se, ymax = value),width=0) + | ||
+ | geom_line(aes(linetype = DM), position=pd) + | ||
+ | #geom_segment for caps | ||
+ | geom_segment(data=with(xc,xc[which(DM=='D'),]), | ||
+ | aes(y=value+se,yend=value+se,x= as.numeric(substr(key, 7, 7))-0.1,xend= as.numeric(substr(key, 7, 7))+0.1)) + | ||
+ | geom_segment(data=with(xc,xc[which(DM=='N'),]), | ||
+ | aes(y=value-se,yend=value-se,x= as.numeric(substr(key, 7, 7))-0.1,xend= as.numeric(substr(key, 7, 7))+0.1)) + | ||
+ | ylim(230, 300) + | ||
+ | ggtitle("Figure 3") + | ||
+ | theme(plot.title = element_text(hjust = 0.5)) | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
https://stackoverflow.com/questions/44194700/selecting-direction-of-multiple-error-bars-in-a-line-plot | https://stackoverflow.com/questions/44194700/selecting-direction-of-multiple-error-bars-in-a-line-plot | ||
+ | ===== グラフのtitleを真ん中上に記載 ===== | ||
+ | |||
+ | <code> | ||
+ | library(tidyverse) | ||
+ | |||
+ | tg <- ToothGrowth | ||
+ | tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","dose")) | ||
+ | |||
+ | ggplot(tgc, aes(x=dose, y=len, shape=supp)) + | ||
+ | theme_set(theme_classic()) + | ||
+ | |||
+ | ylim(0, 40) + | ||
+ | | ||
+ | ggtitle("Figure 3") + | ||
+ | theme(plot.title = element_text(hjust = 0.5)) | ||
+ | |||
+ | </code> | ||
===== リンク ===== | ===== リンク ===== | ||
+ | |||
+ | |||
+ | |||
+ | https://heavywatal.github.io/rstats/ggplot2.html | ||
+ | |||
+ | |||
+ | |||
+ | ===== リンク(英語) ===== | ||
https://mrunadon.github.io/ThesisPlot/ | https://mrunadon.github.io/ThesisPlot/ |