ggstatsplot繪圖|統計+可視化,學術科研神器

  • 2019 年 10 月 30 日
  • 筆記

 

本文首發於“生信補給站”公眾號,https://mp.weixin.qq.com/s/zdSit97SOEpbnR18ARzixw

更多關於R語言,ggplot2繪圖,生信分析的內容,敬請關注小號。

 

ggstatsplotggplot2包的擴展包,可以同時輸出美觀的圖片和統計分析結果,對於經常做統計分析或者生信人來說非常有用。

一 準備數據

gapminder 數據集包含1952到2007年間(5年間隔)的142個國家的life expectancy, GDP per capita, 和 population資訊

#載入繪圖R包
library(ggstatsplot)
#載入gapminder 數據集

library(gapminder)
head(gapminder)

ggstatsplot-R包含有很多繪圖函數(文末會給出),本文僅展示ggbetweenstats函數使用方法。

 

二 ggbetweenstats 繪圖

1 基本繪圖展示

顯示2007年每個continent的預期壽命分布情況,並統計一下不同大陸之間平均預期壽命的是否有差異?差異是否顯著?

#設置種子方便復現
set.seed(123)
# Oceania數據太少,去掉後分析
ggstatsplot::ggbetweenstats(
 data = dplyr::filter(
   .data = gapminder::gapminder,
   year == 2007, continent != "Oceania"
),
 x = continent,
 y = lifeExp,
 nboot = 10,
 messages = FALSE
)

可以看到圖中展示出了2007年每個continent的預期壽命分布的箱線圖,點圖和小提琴圖,均值,樣本數;並且圖形最上方給出了模型的一些統計量資訊(整體)。

統計資訊意義如下圖所示:

註:該函數根據分組變數中的個數自動決定是選擇獨立樣本t檢驗(2組)還是單因素方差分析(3組或更多組)

 

2 添加統計值

上方給出了整體的檢驗P值,下面兩兩之間比較,並添加檢驗統計量

set.seed(123)
ggstatsplot::ggbetweenstats(
 data = dplyr::filter(
   .data = gapminder::gapminder,year == 2007, continent != "Oceania"),
 x = continent,y = lifeExp,
 nboot = 10,
 messages = FALSE,
 effsize.type = "unbiased", # type of effect size (unbiased = omega)
 partial = FALSE, # partial omega or omega?
 pairwise.comparisons = TRUE, # display results from pairwise comparisons
 pairwise.display = "significant", # display only significant pairwise comparisons
 pairwise.annotation = "p.value", # annotate the pairwise comparisons using p-values
 p.adjust.method = "fdr", # adjust p-values for multiple tests using this method
)

3 圖形美化

#添加標題和說明,x軸和y軸標籤,標記,離群值,更改主題以及調色板。

set.seed(123)
# plot
gapminder %>% # dataframe to use
 ggstatsplot::ggbetweenstats(
   data = dplyr::filter(.data = ., year == 2007, continent != "Oceania"),
   x = continent, # grouping/independent variable
   y = lifeExp, # dependent variables
   xlab = "Continent", # label for the x-axis
   ylab = "Life expectancy", # label for the y-axis
   plot.type = "boxviolin", # type of plot ,"box", "violin", or "boxviolin"
   type = "parametric", # type of statistical test , p (parametric), np ( nonparametric), r(robust), bf (Bayes Factor).
   effsize.type = "biased", # type of effect size
   nboot = 10, # number of bootstrap samples used
   bf.message = TRUE, # display bayes factor in favor of null hypothesis
   outlier.tagging = TRUE, # whether outliers should be flagged
   outlier.coef = 1.5, # coefficient for Tukey's rule
   outlier.label = country, # label to attach to outlier values
   outlier.label.color = "red", # outlier point label color
   mean.plotting = TRUE, # whether the mean is to be displayed
   mean.color = "darkblue", # color for mean
   messages = FALSE, # turn off messages
   ggtheme = ggplot2::theme_gray(), # a different theme
   package = "yarrr", # package from which color palette is to be taken
   palette = "info2", # choosing a different color palette
   title = "Comparison of life expectancy across continents (Year: 2007)",
   caption = "Source: Gapminder Foundation"
) + # modifying the plot further
 ggplot2::scale_y_continuous(
   limits = c(35, 85),
   breaks = seq(from = 35, to = 85, by = 5)
)

三 其他繪圖函數

Function Plot Description
ggbetweenstats violin plots for comparisons between groups/conditions
ggwithinstats violin plots for comparisons within groups/conditions
gghistostats histograms for distribution about numeric variable
ggdotplotstats dot plots/charts for distribution about labeled numeric variable
ggpiestats pie charts for categorical data
ggbarstats bar charts for categorical data
ggscatterstats scatterplots for correlations between two variables
ggcorrmat correlation matrices for correlations between multiple variables
ggcoefstats dot-and-whisker plots for regression models

 

四 更多請參照官方文檔

https://indrajeetpatil.github.io/ggstatsplot/index.html

 

◆ ◆ ◆ ◆ ◆

R|生存分析(1):生存分析介紹以及繪製KM曲線

Nomogram(諾莫圖) | Logistic、Cox生存分析結果可視化

Forest plot(森林圖) | Cox生存分析可視化

maftools|TCGA腫瘤突變數據的匯總,分析和可視化

maftools | 從頭開始繪製發表級oncoplot(瀑布圖)

ggalluvial|炫酷桑基圖(Sankey),你也可以秀

ggplot2|詳解八大基本繪圖要素

ggplot2|ggpubr進行“paper”組圖合併

pheatmap|暴雨暫歇,“熱圖”來襲!!!

ggplot2-plotly|讓你的火山圖“活”過來

ggplot2| 繪製KEGG氣泡圖

ggplot2|繪製GO富集柱形圖

繪圖系列|R-corrplot相關圖

繪圖系列|R-VennDiagram包繪製韋恩圖

R|clusterProfiler-富集分析

 

【覺得不錯,右下角點個“在看”,期待您的轉發,謝謝!】