使用ggpubr繪製出版級別的圖形

2021-03-02 統計世界

本文主要學習和介紹:

比較兩組或者多組數據的平均值

然後自動添加P值或者顯著性水平到ggplot的各種圖形中(such as box plots, dot plots, bar plots and line plots …)

一、比較均值的方法

The standard methods to compare the means of two or more groups in R, have been largely described at: comparing means in R.

常用的幾種比較方法如下:

MethodsR functionDescriptionT-testt.test()Compare two groups (parametric)Wilcoxon testwilcox.test()Compare two groups (non-parametric)ANOVAaov() or anova()Compare multiple groups (parametric)Kruskal-Walliskruskal.test()Compare multiple groups (non-parametric)

如何使用這些方法進行計算和對結果進行解釋,可以參考以下連結:

Comparing one-sample mean to a standard known mean:

One-Sample T-test (parametric)

One-Sample Wilcoxon Test (non-parametric)

Comparing the means of two independent groups:

Unpaired Two Samples T-test (parametric)

Unpaired Two-Samples Wilcoxon Test (non-parametric)

Comparing the means of paired samples:

Paired Samples T-test (parametric)

Paired Samples Wilcoxon Test (non-parametric)

Comparing the means of more than two groups

Analysis of variance (ANOVA, parametric):

One-Way ANOVA Test in R

Two-Way ANOVA Test in R

Kruskal-Wallis Test in R (non parametric alternative to one-way ANOVA)

二、R中添加P值的函數介紹

這裡主要使用的是ggpubr包的的兩個函數:

1. compare_means函數

compare_means函數用於比較輸出一組或者多組的均值,使用不同的方法

compare_means(formula, data, method = "wilcox.test", paired = FALSE,
  group.by = NULL, ref.group = NULL, ...)

formula: 畫圖的公式y ~ x。

a formula of the form x ~ group, where x is a numeric variable and group is a factor with one or multiple levels. For example, formula = TP53 ~ cancer_group. It’s also possible to perform the test for multiple response variables at the same time. For example, formula = c(TP53, PTEN) ~ cancer_group.

data: 使用的數據。

a data.frame containing the variables in the formula.

method: 使用的檢驗P值的方法。the type of test. Default is 「wilcox.test」. Allowed values include:

「t.test」 (parametric) and 「wilcox.test」" (non-parametric). Perform comparison between two groups of samples. If the grouping variable contains more than two levels, then a pairwise comparison is performed.

「anova」 (parametric) and 「kruskal.test」 (non-parametric). Perform one-way ANOVA test comparing multiple groups.

paired: 是否做配對檢驗。

a logical indicating whether you want a paired test. Used only in t.test and in wilcox.test.

group.by: 指定分組做P值檢驗。variables used to group the data set before applying the test. When specified the mean comparisons will be performed in each subset of the data formed by the different levels of the group.by variables.

ref.group: 比較均值時指定參考的組,就像比較case-control的數據時,指定control組為對照一樣,指定一個組做為參照。

a character string specifying the reference group. If specified, for a given grouping variable, each of the group levels will be compared to the reference group (i.e. control group). ref.group can be also 「.all.」. In this case, each of the grouping variable levels is compared to all (i.e. base-mean). 這裡的base-mean指的就是所有組的平均值

2.stat_compare_means函數

功能與上一個函數相似,只是這個函數用於在ggplot圖形中添加平均值比較的P值或者顯著性水平

stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE,
                   label = NULL,  label.x = NULL, label.y = NULL,  ...)

mapping: 和ggplot函數一樣指定aes()映射。

Set of aesthetic mappings created by aes().

comparisons: 由兩個向量組成的列表,這兩個向量就是要對比組的名字或者index值。

A list of length-2 vectors. The entries in the vector are either the names of 2 values on the x-axis or the 2 integers that correspond to the index of the groups of interest, to be compared.

hide.ns: 邏輯值,默認為FALSE。是否顯示不顯著時的符號ns。

logical value. If TRUE, hide ns symbol when displaying significance levels.

label: 指定標籤的類型,可以是P值(「p.format」)或者是顯著性水平(「p.signif」)(用*號和ns表示)。

character string specifying label type. Allowed values include 「p.signif」 (shows the significance levels), 「p.format」 (shows the formatted p value).

label.x,label.y: 指定label的坐標位置。

numeric values. coordinates (in data units) to be used for absolute positioning of the label. If too short they will be recycled.

: 其他的參數。

other arguments passed to the function compare_means() such as method, paired, ref.group.

使用boxplot演示添加P值和顯著性水平1. 準備

# 加載包
# library(tidyverse) #非必須
library(ggpubr)

# 示例數據:ToothGrowth
data("ToothGrowth")
head(ToothGrowth)

head(ToothGrowth) %>% knitr::kable(format = "markdown")

lensuppdose4.2VC0.511.5VC0.57.3VC0.55.8VC0.56.4VC0.510.0VC0.52. 獨立雙樣本比較

compare_means(len ~ supp, data = ToothGrowth) %>% knitr::kable()

.y.group1group2pp.adjp.formatp.signifmethodlenOJVC0.06449070.0640.064nsWilcoxon

Returned value is a data frame with the following columns:

.y.: the y variable used in the test.
p: the p-value
p.adj: the adjusted p-value. Default value for p.adjust.method = 「holm」
p.format: the formatted p-value
p.signif: the significance level.
method: the statistical test used to compare groups.

Create a box plot with p-values:

p <- ggboxplot(data = ToothGrowth, 
               x = "supp", y = "len",
               color = "supp", palette = "jco",
               add = "jitter")
# add = "jitter"參數添加箱線圖的數值點
# 添加p值
p + stat_compare_means()
# 再畫一幅,這次改變P值的統計方法
p + stat_compare_means(method = "t.test")

顯示P值的標籤位置可以通過如下參數來調整:label.x, label.y。

顯示P值的標籤默認為 compare_means() 返回值中的 method 和 p 的組合。也可以通過 aes() 函數指定為其他顯示形式。例如:

For example,

aes(label = ..p.format..) or aes(label = paste0(「p =」, ..p.format..)): display only the formatted p-value (without the method name)
aes(label = ..p.signif..): display only the significance level.
aes(label = paste0(..method.., 「\n」, 「p =」, ..p.format..)): Use line break (「\n」) between the method name and the p-value.

如:

p + stat_compare_means(aes(label = paste0(..method..,": ","p=",..p.format..)),
                       label.x = 1.5, label.y = 40, method = "t.test")

# 單獨顯示字符型向量的顯著水平
p + stat_compare_means(label = "p.signif", label.x = 1.5, label.y = 40)

## 單獨顯示P值
p + stat_compare_means(label = "p.format",
                       label.x = 1.5, label.y = 40, method = "t.test")

# 使用aes映射也可以達到同樣的效果,此時只有P值,需要連接P=
p + stat_compare_means(aes(label = paste("p = ",..p.format..)),
                       label.x = 1.5, label.y = 40, method = "t.test")

3. 比較兩個配對樣本

compare_means(len ~ supp, data = ToothGrowth, paired = TRUE)

.y.group1group2pp.adjp.formatp.signifmethodlenOJVC0.00431260.00430.0043**Wilcoxon

# 使用ggpaired函數可視化配對數據
ggpaired(data = ToothGrowth, 
         x = "supp", y = "len",
         color = "supp", line.color = "gray",
         line.size = 0.4, palette = "jco") +
  stat_compare_means(paired = T)

4.比較多組樣本

1. 全局比較

# 使用anova的方法, 全局比較
compare_means(len ~ dose,  data = ToothGrowth, method = "anova")

.y.pp.adjp.formatp.signifmethodlen009.5e-16****Anova

# Default method = "kruskal.test" for multiple groups
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "jco")+
  stat_compare_means()
# Change method to anova
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "jco")+
  stat_compare_means(method = "anova")

2. 成對比較

如果分組超過兩個,默認將所有組兩兩比較, 默認方法為wilcox.test, 可以改為t.test

compare_means(len ~ dose,  data = ToothGrowth)

.y.group1group2pp.adjp.formatp.signifmethodlen0.510.00000701.4e-057.0e-06****Wilcoxonlen0.520.00000012.0e-078.4e-08****Wilcoxonlen120.00017721.8e-040.00018***Wilcoxon

# 可視化所指定的比較
# 指定比較的組,以下3組都指定
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )

ggboxplot(data = ToothGrowth, 
          x = "dose", y = "len",
          color = "dose", palette = "jco") +
  stat_compare_means(comparisons = my_comparisons) + # 指定成對比較, 並指定P值位置
  stat_compare_means(label.y = 50) # 指定P值位置的這句代碼不能和上面的同一個函數的代碼合併,合併之後P值比價會顯示在一條線段上

# 將分組比較的P值label使用label.y指定位置
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "jco")+ 
  stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))
# 去掉全局比較的數值

3. 指定一個參考組的兩兩比較

# Pairwise comparison against reference
compare_means(len ~ dose,  data = ToothGrowth,
              # 指定dose中0.5的組作為參考組
              ref.group = "0.5",
              method = "t.test") %>% 
  knitr::kable(format = "markdown")

.y.group1group2pp.adjp.formatp.signifmethodlen0.511e-071e-071.3e-07****T-testlen0.520e+000e+004.4e-14****T-test

# Visualize
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "jco")+
  stat_compare_means(method = "anova", label.y = 40)+      # 全局比較,使用anova方法
  stat_compare_means(label = "p.signif", method = "t.test",
                     ref.group = "0.5")                    # 指定配對比價的refer group,比較對象為0.5的組

4. 相對於所有組的均值(base-mean)的多重兩兩比較的檢驗

# Comparison of each group against base-mean
compare_means(len ~ dose,  data = ToothGrowth, ref.group = ".all.",
              method = "t.test")

.y.group1group2pp.adjp.formatp.signifmethodlen.all.0.50.00000039.0e-072.9e-07****T-testlen.all.10.51187735.1e-010.51nsT-testlen.all.20.00000049.0e-074.3e-07****T-test

# .all.參數比較其中一組與所有的組的均值(base-mean),在組比較多時,比較方便
ggboxplot(data = ToothGrowth, 
          x = "dose", y = "len",
          color = "dose", palette = "jco")+
  stat_compare_means(method = "kruskal.test", label.y = 40)+
  stat_compare_means(label = "p.format", method = "t.test", ref.group = ".all.") 

下面使用骨髓瘤的數據來說明和所有樣本的均值(.all.)比較在一些典型的情況中是很有用的。

將依據患者molecular進行分組,繪製各個組別DEPDC1基因的表達水平。目的是比較各個組別之間是否存在差別,如果有差別,差別又在哪裡。

要回答以上問題,可以在所有7個組之間進行兩兩比較(pairwise comparison)。由於組別較多,將導致很多種組別組合,這將很難解釋。

一個簡單的解決辦法是將7組中的每一組與「.all.」(如base-mean)比較。當檢驗結果顯著時,可以得出結論:與所有組相比,xxx組的DEPDC1的表達水平顯著降低或顯著升高。

# Load myeloma data from GitHub
myeloma <- read.delim("https://raw.githubusercontent.com/kassambara/data/master/myeloma.txt")

# Perform the test
compare_means(DEPDC1 ~ molecular_group,  data = myeloma,
              ref.group = ".all.", method = "t.test")

.y.group1group2pp.adjp.formatp.signifmethodDEPDC1.all.Cyclin D-10.28775291.0e+000.29nsT-testDEPDC1.all.Cyclin D-20.42442401.0e+000.42nsT-testDEPDC1.all.MMSET0.57841931.0e+000.58nsT-testDEPDC1.all.MAF0.25381261.0e+000.25nsT-testDEPDC1.all.Hyperdiploid0.00000002.0e-072.7e-08****T-testDEPDC1.all.Proliferation0.00002391.2e-042.4e-05****T-testDEPDC1.all.Low bone disease0.00000533.2e-055.3e-06****T-test

head(myeloma)

Xmolecular_groupchr1q21_statustreatmenteventtimeCCND1CRIM1DEPDC1IRF4TP53WHSC1groupGSM50986Cyclin D-13 copiesTT2069.249908.4420.9523.516156.510.0261.9cyclin.d.1GSM50988Cyclin D-22 copiesTT2066.4316698.852.021.116946.21056.9363.8cyclin.d.2GSM50989MMSET2 copiesTT2066.50294.5617.9192.98903.91762.810042.9mmsetGSM50990MMSET3 copiesTT2142.67241.911.9184.711894.7946.84931.0mmsetGSM50991MAFNATT2065.00472.638.8212.07563.1361.4165.0mafGSM50992Hyperdiploid2 copiesTT2065.20664.116.9341.616023.42096.3569.2hyperdiploid

# 可視化
# legend = "none"參數去掉圖形上方顯示的每種顏色的box代表哪種分組的信息

ggboxplot(myeloma, x = "molecular_group", y = "DEPDC1", color = "molecular_group", 
          add = "jitter", legend = "none") +
  rotate_x_text(angle = 45)+
  geom_hline(yintercept = mean(myeloma$DEPDC1), linetype = 2)+ # Add horizontal line at base mean
  stat_compare_means(method = "anova", label.y = 1600)+        # Add global annova p-value
  stat_compare_means(label = "p.signif", method = "t.test",
                     ref.group = ".all.")                      # Pairwise comparison against all, 此處添加hide.ns = TRUE參數,將隱藏不顯著的ns符號

From the plot above, we can conclude that DEPDC1 is significantly overexpressed in proliferation group and, it’s significantly downexpressed in Hyperdiploid and Low bone disease compared to all.

5. 多個分組變量

compare_means(len ~ supp, data = ToothGrowth, 
              group.by = "dose")

dose.y.group1group2pp.adjp.formatp.signifmethod0.5lenOJVC0.02318640.0460.023*Wilcoxon1.0lenOJVC0.00403040.0120.004**Wilcoxon2.0lenOJVC1.00000001.0001.000nsWilcoxon

Visualize (1/2). Create a multi-panel box plots facetted by group (here, 「dose」):

# 多個小圖facet
p <- ggboxplot(data = ToothGrowth, 
               x = "supp", y = "len",
               color = "supp", palette = "jco",
               add = "jitter",
               facet.by = "dose",
               short.panel.labs = F) # short.panel.labs默認為T,改為F顯示分組dose:

# 僅顯示P值,去除方法
p + stat_compare_means(label = "p.format")

# 或者顯示顯著性符號,而不是直接顯示P值
p + stat_compare_means(label =  "p.signif", label.x = 1.5)

# To hide the 『ns』 symbol, use the argument hide.ns = TRUE.

Visualize (2/2). Create one single panel with all box plots. Plot y = 「len」 by x = 「dose」 and color by 「supp」:

# 將所有小圖組合放在一張圖裡
p <- ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "supp", palette = "jco",
          add = "jitter") # 去掉facet.by參數
p + stat_compare_means(aes(group = supp))
# 使用aes映射時,列名不能加雙引號

# 只顯示P值
p + stat_compare_means(aes(group = supp),label = "p.format")

顯示P=數值

# 單純的只顯示P的數值
p + stat_compare_means(aes(group = supp, label = paste(..p.format..)))

只顯示P值

# 只顯示顯著性水平
p + stat_compare_means(aes(group = supp), label = "p.signif")

compare_means(len ~ supp, data = ToothGrowth, 
              group.by = "dose", paired = TRUE) # 增加paired參數

dose.y.group1group2pp.adjp.formatp.signifmethod0.5lenOJVC0.03296940.0660.033*Wilcoxon1.0lenOJVC0.01367190.0410.014*Wilcoxon2.0lenOJVC1.00000001.0001.000nsWilcoxon

相比於非配對P檢驗,配對P檢驗的P值更大

# Box plot facetted by "dose"
p <- ggpaired(ToothGrowth, x = "supp", y = "len",
          color = "supp", palette = "jco", 
          line.color = "gray", line.size = 0.4,
          facet.by = "dose", short.panel.labs = FALSE)
# Use only p.format as label. Remove method name.
p + stat_compare_means(label = "p.format", paired = TRUE)

6. 其他類型的圖表6.1 不分組的圖

# 條形圖
ggbarplot(data = ToothGrowth, x = "dose", y = "len", add = "mean_se",
          color = "dose",palette="jco", legend="none")+
  stat_compare_means()+ # 總體比較
  stat_compare_means(ref.group = "0.5", label = "p.signif", label.y = c(22,28))

# Line plot of mean +/-se
ggline(ToothGrowth, x = "dose", y = "len", add = "mean_se",
          color = "black", linetype = 2)+
  stat_compare_means() +                                         # Global p-value
  stat_compare_means(ref.group = "0.5", label = "p.signif",
                     label.y = c(22, 29)) 

6.2 分組的圖

# 分組條形圖
# position = position_dodge()調節supp分組的OJ組和VC組兩組樣本的柱狀圖的距離,默認值為重合(0)
ggbarplot(ToothGrowth, x = "dose", y = "len", add = "mean_se",
          color = "supp", palette = "jco", 
          position = position_dodge(0.8))+
  stat_compare_means(aes(group = supp), label = "p.signif", label.y = 29)

# 分組折線圖
ggline(ToothGrowth, x = "dose", y = "len", add = "mean_se",
          color = "supp", palette = "jco")+
  stat_compare_means(aes(group = supp), label = "p.signif", 
                     label.y = c(16, 25, 29))

參考:

數據可視化——R語言為ggplot圖形添加P值和顯著性水平

ggpubr: Publication Ready Plots Add P-values and Significance Levels to ggplots

查看原文獲取更佳的排版顯示內容

相關焦點

  • ggpubr:快速繪製用於發表的圖形
    與R的基礎繪圖系統相比,基於grid繪圖系統的ggplot2已經在語法理解性上已經進步很多,但是通過ggplot2繪製用於學術雜誌的圖形,仍然需要較多的繪圖函數(或者加載一些寫好的模板代碼)。為此Alboukadel Kassambara基於ggplot2、ggsci包開發了ggpubr用於繪製符合出版物要求的圖形。
  • 淺談生態學領域科研圖表繪製的方向和未來
    ggplot的出現讓圖形的繪製更加簡單。人們更加追求組合圖形的繪製。尤其是大文章,對組合圖形的熱度不減。科研圖表複雜度提高和色彩運用更加注重美學特徵,這已經相當明顯了,之後會有一篇帖子專門論述這兩個特徵。今天我們來談談以上的三個特徵。那就是組合圖表熱。
  • R語言統計與繪圖:ggplot2圖形組合布局
    在科研論文中,有時我們需要繪製幾張圖形,並將這幾張圖形整合到一張大圖上面。前面我們學習了基礎繪圖包怎麼組合布局圖形,今天來學習兩個新函數,看ggplot2繪製的圖形怎麼組合。ggplot2組合圖形布局可以使用Rmisc包的multiplot()函數,也可以使用ggpubr包的ggarrange()函數。1.
  • ggplot2學習筆記之圖形排列
    繪製圖形#load packageslibrary(gridExtra)library(cowplot)library(ggpubr)#dataset ToothGrowth and mtcarsmtcars$name <- rownames(mtcars)mtcars$cyl <- as.factor(mtcars$cyl)head(mtcars[, c("name
  • ggplot2|繪製GO富集柱形圖
    本文利用R語言的ggplot2包,從頭帶您繪製可發表級別的GO富集分析結果圖。利用各種生信工具得到富集分析結果,數據列可能不一致,但關鍵幾列都有。library(ggplot2)data <- read.csv("GO_enrichment_significant.csv",header=TRUE)head(data)參照之前ggplot2使用方法,更改geom即可繪製簡單的bar圖,按照
  • 基於ggplot2包繪製SCI學術散點圖,又是保姆級教程
    本期推文,我們使用ggplot2包繪製學術擬合散點圖。1. 加載數據數據在後臺回復散點圖即可獲取。繪製簡單圖形我們首先使用ggplot2的基本設置對數據進行散點繪製,這裡散點形狀 shape=15 為黑色方塊。
  • 手把手教你用graphpad作圖:重複測量方差分析的圖形繪製
    統計分析採用重複測量資料的方差分析,統計圖使用Prism Graphpad繪製。1、使用SPSS軟體計算出每組數據的均值和標準,整理到Excel中。可以參考本公眾號之前的文章《手把手教你用Graphpad+SPSS輕鬆完成分組柱形圖》。
  • 如何使用 origin 繪製雙Y軸圖形?
    雙Y軸圖(Double-Y)圖形模板主要適用於試驗數據中自變量數據相同,但有兩個因變量的情況。 繪製雙Y軸圖形的原因是有兩個以上的Y列數據,它們共有區間接近的X軸坐標,但Y軸坐標的數值範圍相差很大。如X軸為時間,兩個Y軸分別為數值和百分比。
  • 如何使用ug繪製出幾何圖形?詳見操作過程
    ug系列軟體最新版本下載Unigraphics NX(UG NX)軟體版本:8.0 官方版行業軟體立即查看UG NX 10.0軟體版本:官方中文版圖形圖像立即查看剛接觸ug的朋友可能還不會繪製幾何圖形,今天小編就講解使用ug繪製幾何圖形的操作過程
  • R之箱線圖繪製
    16s分析一直在連載,但是最基礎的莫過於alpha多樣性了,但是箱線圖卻不是alpha多樣性的唯一選擇,箱線圖也不是局限於alpha多樣性,這裡藉助alpha多樣性,將箱線圖做一個完整繪製
  • 使用R繪製幾種常用的雙坐標軸圖形
    之前公眾號推送了一系列關於使用ggplot2包繪製統計圖形的文章,有網友詢問是否可以繪製雙軸的統計圖形。
  • Word基礎知識之繪製圖形
    Word中的繪圖是指一個或一組圖形對象(包括形狀、圖表、流程圖、線條和藝術字等),可以直接選用相應工具在文檔中繪製圖形,並通過顏色、邊框或其他效果對其進行設置。1、使用繪圖畫布向Word文檔插人圖片、圖形對象時,可以將圖片、圖形等對象放置在繪圖畫布中。繪圖畫布在繪圖和文檔的其他部分之間提供了一條框架式的邊界。在默認情況下,繪圖畫布沒有背景或邊框,但是如同處理圖形對象一樣,可以對繪圖畫布進行格式設置。
  • 都9102年了,還不會使用CSS繪製圖形?
    其實使用CSS可以繪製出很形狀的,比如三角形,梯形,圓形,橢圓形等等,並不只能畫矩形。 今天的教程,就來教大家怎麼去用CSS繪製這些圖形吧。 為了方便大家去理解,今天分成基本形狀和組合形狀來一起講解吧,基本形狀是非常好理解的,再利用這些基本形狀進行組合,就可以實現稍微複雜的組合形狀了。
  • CorelDRAW:CDR-如何繪製基礎圖形(下)
    還有一種是複雜星形,選擇工具箱中的「複雜星形工具」,在屬性欄中設置「點數或邊數工具」和「銳度工具」數值,然後在繪製區按住滑鼠左鍵並拖曳,釋放滑鼠後即可得到複雜星形圖形。3、圖紙的使用使用「圖紙工具」可以繪製出不同行/列數的網格對象。
  • AI科研繪圖(一):零基礎入門和基本圖形繪製
    Adobe illustrator是一種應用於出版、多媒體和在線圖像的工業標準矢量插畫的軟體,是一款非常好的圖片處理工具,簡稱AI。
  • 第三章基本圖形的繪製和曲線的繪製及編輯
    曲線繪製工具1、手繪手繪這個工具的快捷鍵是F5,這個工具就是按住滑鼠左鍵不鬆開,就可以任意繪製自己想要的圖形,是比較隨意的,所以用的比較少,2、2點線這個工具就是通過兩點繪製一條直線,我們其實用的不多,只要按住滑鼠左鍵不松就可以繪製出一條直線,按住shift可以繪製豎線,如下圖所示。
  • 如何用Matlab繪製圖形
    在MATLAB中繪製簡單函數的圖形有以下幾個步驟:1.先定義自變量x,給定自變量x的定義域