本文首發(fā)于“生信補給站”公眾號苫耸,https://mp.weixin.qq.com/s/zdSit97SOEpbnR18ARzixw
更多關(guān)于R語言夹姥,ggplot2繪圖杉武,生信分析的內(nèi)容,敬請關(guān)注小號辙售。
ggstatsplot
是ggplot2
包的擴展包轻抱,可以同時輸出美觀的圖片和統(tǒng)計分析結(jié)果,對于經(jīng)常做統(tǒng)計分析或者生信人來說非常有用旦部。
一 準備數(shù)據(jù)
gapminder 數(shù)據(jù)集包含1952到2007年間(5年間隔)的142個國家的life expectancy, GDP per capita, 和 population信息
#載入繪圖R包
library(ggstatsplot)
#載入gapminder 數(shù)據(jù)集
library(gapminder)
head(gapminder)
ggstatsplot-R包含有很多繪圖函數(shù)(文末會給出)祈搜,本文僅展示ggbetweenstats函數(shù)使用方法。
二 ggbetweenstats 繪圖
1 基本繪圖展示
顯示2007年每個continent的預期壽命分布情況士八,并統(tǒng)計一下不同大陸之間平均預期壽命的是否有差異容燕?差異是否顯著?
#設置種子方便復現(xiàn)
set.seed(123)
# Oceania數(shù)據(jù)太少婚度,去掉后分析
ggstatsplot::ggbetweenstats(
data = dplyr::filter(
.data = gapminder::gapminder,
year == 2007, continent != "Oceania"
),
x = continent,
y = lifeExp,
nboot = 10,
messages = FALSE
)
可以看到圖中展示出了2007年每個continent的預期壽命分布的箱線圖蘸秘,點圖和小提琴圖,均值蝗茁,樣本數(shù)醋虏;并且圖形最上方給出了模型的一些統(tǒng)計量信息(整體)。
統(tǒng)計信息意義如下圖所示:
注:該函數(shù)根據(jù)分組變量中的個數(shù)自動決定是選擇獨立樣本t檢驗(2組)還是單因素方差分析(3組或更多組)
2 添加統(tǒng)計值
上方給出了整體的檢驗P值评甜,下面兩兩之間比較灰粮,并添加檢驗統(tǒng)計量
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軸標簽忍坷,標記粘舟,離群值,更改主題以及調(diào)色板佩研。
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)
)
三 其他繪圖函數(shù)
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生存分析結(jié)果可視化
maftools|TCGA腫瘤突變數(shù)據(jù)的匯總,分析和可視化
maftools | 從頭開始繪制發(fā)表級oncoplot(瀑布圖)
ggalluvial|炫酷裳恚基圖(Sankey)晰骑,你也可以秀
【覺得不錯骤公,右下角點個“在看”抚官,期待您的轉(zhuǎn)發(fā),謝謝阶捆!】