ユーザ用ツール

サイト用ツール


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

差分

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

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
ggplot2で論文用の白黒折れ線グラフ [2018/08/13] – [開発環境] adash333ggplot2で論文用の白黒折れ線グラフ [2020/08/12] (現在) – [ggplot2で論文用の白黒折れ線グラフ] adash333
行 1: 行 1:
 ====== ggplot2で論文用の白黒折れ線グラフ ====== ====== ggplot2で論文用の白黒折れ線グラフ ======
 +
 +[[初めての医療統計:index.html|初めての医療統計 目次]]
  
 ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。 ggplot2(tidyverse)では、カラーのきれいなグラフを出力してくれるのはありがたいのですが、論文用には白黒のグラフを作成したい場合が多いです。
  
 基本的には、<wrap em>ggplot()+theme_set(theme_classic())</wrap>を用いるのですが、具体的な方法を記載させていただきたいと思います。 基本的には、<wrap em>ggplot()+theme_set(theme_classic())</wrap>を用いるのですが、具体的な方法を記載させていただきたいと思います。
 +
 +===== ソースコード =====
 +
 +https://colab.research.google.com/drive/1yPHODB6sgUU9OJx48tEibAF9rnUHDv-s?usp=sharing
 +
 +
 +
 +
 +
  
 ===== 開発環境 ===== ===== 開発環境 =====
行 20: 行 31:
 参考:http://twosquirrel.mints.ne.jp/?p=26918 参考:http://twosquirrel.mints.ne.jp/?p=26918
  
 +===== Rmdファイルの新規作成と保存 =====
 +File > New File > R Notebook で、R Notebookファイルを新規作成。
  
-===== 1.サマリー作成用の関数の定義 ===== +{{:pasted:20180813-232234.png}}
-ggplot2()でグラフを記載する前に、データを整理する必要があります。+
  
-Plotting means and error bars (ggplot2)\\ +{{:pasted:20180813-232303.png}}
-http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/+
  
-<code> +以下のようにR Notebookファイルが作成されるので、
-summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE, +
-                      conf.interval=.95, .drop=TRUE) { +
-    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       length(x) 
-    } 
  
-    # This does the summary. For each group's data frame, return a vector with 
-    # N, mean, and sd 
-    datac <- ddply(data, groupvars, .drop=.drop, 
-      .fun = function(xx, col) { 
-        c(N    = length2(xx[[col]], na.rm=na.rm), 
-          mean = mean   (xx[[col]], na.rm=na.rm), 
-          sd   = sd     (xx[[col]], na.rm=na.rm) 
-        ) 
-      }, 
-      measurevar 
-    ) 
  
-    # Rename the "mean" column     +===== 1.グラフ作成用に平均と標準偏差を計算 ===== 
-    datac <- rename(datac, c("mean" = measurevar))+ggplot2()でグラフを記載する前に、データを整理する必要があります。
  
-    datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean +dplyr(tidyverse)の 
- +  -<wrap hi>group_by()関数</wrap> と 
-    # Confidence interval multiplier for standard error +  -<wrap hi>summarise()関数</wrap
-    # Calculate t-statistic for confidence interval:  +を用いて、ループごとに、個数、平均、標準偏差を計算して新しい表を作成します。
-    # 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) +
-+
-</code+
- +
-===== 2.白黒の折れ線ラフの作成 =====+
  
 <code> <code>
 library(tidyverse) library(tidyverse)
  
-tg <- ToothGrowth +head(ToothGrowth
-tgc <- summarySE(tgmeasurevar="len"groupvars=c("supp","dose"))+str(ToothGrowth) 
 + 
 +<- ToothGrowth %>% 
 +  group_by(suppdose) %>% 
 +  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)
 +
  
 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)
 +</code>
  
 +===== 2.白黒の折れ線グラフの作成 =====
 +
 +上記で作成した表dを用いて、ggplot()関数でグラフを描きます。
 +
 +<code>
 +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")
 </code> </code>
  
 以下のようなグラフが出力されます。 以下のようなグラフが出力されます。
  
-{{:pasted:20180811-054428.png}}+{{ :ダウンロード.png |}} 
  
 ===== 3.グラフをpngファイルで出力 ===== ===== 3.グラフをpngファイルで出力 =====
行 199: 行 219:
 https://www.safaribooksonline.com/library/view/r-graphics-cookbook/9781449363086/ch04.html https://www.safaribooksonline.com/library/view/r-graphics-cookbook/9781449363086/ch04.html
  
 +===== リンク =====
 +目次:[[初めての医療統計:index.html|初めての医療統計 目次]]
 +
 +前:
 +
 +次:
  
  
  

ggplot2で論文用の白黒折れ線グラフ.1534170063.txt.gz · 最終更新: 2018/10/07 (外部編集)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki