文書の過去の版を表示しています。
ggplot2で論文用の白黒折れ線グラフ
目次
ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。
基本的には、ggplot()+theme_set(theme_classic())を用いるのですが、具体的な方法を記載させていただきたいと思います。
ソースコード
開発環境
2018年8月現在、メールアドレスを登録するのみで、無料です。
RStudio Cloudのconsole画面(画面左下)で、install.packages(“tidyverse”)を入力して、tidyverseパッケージをインストールした状態とします。
インストール終了後の画面
Rmdファイルの新規作成と保存
1.グラフ作成用に平均と標準偏差を計算
ggplot2()でグラフを記載する前に、データを整理する必要があります。
dplyr(tidyverse)の
- group_by()関数 と
- summarise()関数
を用いて、グループごとに、個数、平均、標準偏差を計算して新しい表を作成します。
library(tidyverse)
head(ToothGrowth)
str(ToothGrowth)
d <- ToothGrowth %>%
group_by(supp, dose) %>%
summarise(
n=n(),
mean_len=mean(len),
sd_len=sd(len)
)
# 蛇足だが、以下の書き方はsdがNAになってしまうので注意
# https://dplyr.tidyverse.org/reference/summarise.html
# summarise(n=n(), len=mean(len), sd=sd(len))
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, 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)
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, 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) +
# グラフにタイトルをつける
ggtitle("ToothGrowth") +
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="Length")
以下のようなグラフが出力されます。
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 = "Helvetica")
p3 = p2 + stat_smooth(method = lm, formula = y ~ log(x))
ggsave("mpg-displ-cty.png", p3, width = 4, height = 4, dpi=300)
Rmdファイルと同じフォルダに、以下のようなgraph.pngが保存されます。
エラーバーを片方だけにつける
下記リンク先によりますと、
1. geom_errorbar()でwidth=0のエラーバーを作成 2. geom_segment()で片方だけエラーバーの横線を追加
という流れになります。
geom_segment()でエラーバーのxの始点と終点を指定するときに、
横軸が数字であれば、as.numeric(key)-0.1のように指定する 横軸が文字(00pre, 1month, 2month, 3monthなど)であれば、 keyを0,1,2,3などに置き換えてから(csvファイルを修正しておいてもよいかもしれません)、 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,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))
グラフのtitleを真ん中上に記載
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))
リンク
リンク(英語)
https://mrunadon.github.io/ThesisPlot/
Plotting means and error bars (ggplot2)
http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/
JULY 24, 2016
Line plot for two-way designs using ggplot2
https://drsimonj.svbtle.com/mean-and-ci-plot-for-twoway-designs-using-ggplot2
Chapter 4. Line Graphs
https://www.safaribooksonline.com/library/view/r-graphics-cookbook/9781449363086/ch04.html
リンク







