ユーザ用ツール

サイト用ツール


ggplot2で論文用の白黒折れ線グラフ

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
ggplot2で論文用の白黒折れ線グラフ [2020/08/12] – [ソースコード] adash333ggplot2で論文用の白黒折れ線グラフ [2020/08/12] (現在) – [ggplot2で論文用の白黒折れ線グラフ] adash333
行 1: 行 1:
 ====== ggplot2で論文用の白黒折れ線グラフ ====== ====== ggplot2で論文用の白黒折れ線グラフ ======
 +
 +[[初めての医療統計:index.html|初めての医療統計 目次]]
  
 ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。 ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。
行 40: 行 42:
  
  
-===== 1.サマリー作成用の関数の定義 =====+===== 1.グラフ作成用に平均と標準偏差を計算 =====
 ggplot2()でグラフを記載する前に、データを整理する必要があります。 ggplot2()でグラフを記載する前に、データを整理する必要があります。
  
-Plotting means and error bars (ggplot2)\\ +dplyr(tidyverse) 
-http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/+  -<wrap hi>group_by()関数</wrap> と 
 +  -<wrap hi>summarise()関数</wrap> 
 +を用いて、グループごとに、個数、平均、標準偏差を計算して新しい表を作成します。
  
 <code> <code>
-summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE, +library(tidyverse)
-                      conf.interval=.95, .drop=TRUE) { +
-    library(plyr)+
  
-    # New version of length which can handle NA's: if na.rm==T, don't count them +head(ToothGrowth
-    length2 <- function (x, na.rm=FALSE{ +str(ToothGrowth)
-        if (na.rmsum(!is.na(x)) +
-        else       length(x) +
-    }+
  
-    # This does the summary. For each group's data framereturn a vector with +d <- ToothGrowth %>% 
-    # Nmean, and sd +  group_by(suppdose) %>% 
-    datac <- ddply(data, groupvars, .drop=.drop+  summarise( 
-      .fun function(xx, col{ +    n=n(),  
-        c(N    length2(xx[[col]], na.rm=na.rm), +    mean_len=mean(len),  
-          mean = mean   (xx[[col]], na.rm=na.rm), +    sd_len=sd(len
-          sd   = sd     (xx[[col]], na.rm=na.rm) +  ) 
-        +  # 蛇足だが、以下の書き方はsdがNAになってしまうので注意 
-      }, +  # https://dplyr.tidyverse.org/reference/summarise.html 
-      measurevar +  # summarise(n=n(), len=mean(len), sd=sd(len)) 
-    )+
  
-    # Rename the "mean" column     +pd <- position_dodge(0.1# move them .05 to the left and right
-    datac <- rename(datac, c("mean" = measurevar))+
  
-    datac$se <- datac$sd / sqrt(datac$N # Calculate standard error of the mean +ggplot(d, aes(x=dose, y=mean_len, shape=supp)) + 
- +    theme_set(theme_classic()) + 
-    # Confidence interval multiplier for standard error +    geom_point(size=4position=pd) + 
-    # Calculate t-statistic for confidence interval:  +    geom_errorbar(aes(ymin=mean_len-sd_len, ymax=mean_len+sd_len)width=.1, position=pd+ 
-    # e.g., if conf.interval is .95, use .975 (above/below)and use df=N-1 +    geom_line(aes(linetype = supp), position=pd)
-    ciMult <- qt(conf.interval/.5datac$N-1) +
-    datac$ci <- datac$se * ciMult +
- +
-    return(datac) +
-}+
 </code> </code>
  
 ===== 2.白黒の折れ線グラフの作成 ===== ===== 2.白黒の折れ線グラフの作成 =====
 +
 +上記で作成した表dを用いて、ggplot()関数でグラフを描きます。
  
 <code> <code>
-library(tidyverse) 
- 
-tg <- ToothGrowth 
-tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","dose")) 
- 
 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, position=pd) + +  geom_point(size=4, position=pd) + 
-    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) + 
 +  # グラフにタイトルをつける 
 +  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")
 </code> </code>
  
 以下のようなグラフが出力されます。 以下のようなグラフが出力されます。
  
-{{:pasted:20180811-054428.png}}+{{ :ダウンロード.png |}} 
  
 ===== 3.グラフをpngファイルで出力 ===== ===== 3.グラフをpngファイルで出力 =====

ggplot2で論文用の白黒折れ線グラフ.1597226305.txt.gz · 最終更新: 2020/08/12 by adash333

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki