ggstatsplot绘图|统计+可视化,学术科研神器

简介: ggstatsplot绘图|统计+可视化,学术科研神器

本文首发于“生信补给站”公众号 https://mp.weixin.qq.com/s/zdSit97SOEpbnR18ARzixw


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)
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
相关文章
|
9月前
|
资源调度 自然语言处理 数据可视化
【数据分析与可视化】Matplotlib中十大绘图模型的讲解及实现(图文解释 附源码)
【数据分析与可视化】Matplotlib中十大绘图模型的讲解及实现(图文解释 附源码)
126 1
|
6月前
|
数据可视化 定位技术 Python
在模仿中精进数据可视化03:OD数据的特殊可视化方式
在模仿中精进数据可视化03:OD数据的特殊可视化方式
93 2
|
6月前
|
数据可视化 JavaScript 前端开发
惊世奇迹!D3.js 铸就数据可视化辉煌,探索交互式图表与效果的奇幻之旅!
【8月更文挑战第12天】在数据驱动的时代,D3.js 作为领先的 JavaScript 库,以其强大的功能和灵活性在数据可视化领域脱颖而出。开发者可通过 D3.js 构建精美的交互式图表,如简单的柱状图或具备缩放拖拽功能的折线图,甚至将数据以地图、树形结构等独特形式展现。尽管学习曲线陡峭,但它在数据分析、商业智能等领域的广泛应用使其成为专业人士传达数据故事、辅助决策制定的强大工具。
143 1
|
6月前
|
数据可视化 Python
Python 绘图编程:一场震撼视觉的奇幻之旅,带你闯入绚丽多彩的数据可视化世界!
【8月更文挑战第22天】Python 以强大功能和简洁语法闻名,其绘图能力尤其出色,助力数据可视化与创意展现。常用绘图库 Matplotlib 提供丰富指令,支持多样图表创作,如线图、柱状图及散点图等。通过简单代码即可完成数据展示,包括设置标题、轴标签等,实现直观的数据故事叙述,满足不同场景需求。不断实践探索,创造更佳视觉效果。
48 0
|
7月前
|
数据可视化 数据挖掘 数据处理
数据之美,尽收眼底!Python数据分析师如何利用Matplotlib、Seaborn打造视觉盛宴,征服数据世界?
【7月更文挑战第22天】Python的Matplotlib和Seaborn库是数据可视化的利器。Matplotlib基础强大,灵活定制,适合各类图表;Seaborn在其上层封装,提供美观的统计图形,简化复杂操作。结合使用,它们助数据分析师揭示数据规律,打造视觉盛宴,征服数据世界。示例代码分别展示了如何绘制正弦波图和箱线图。
49 0
|
9月前
|
数据可视化 搜索推荐 大数据
科研绘图与学术图表绘制:从入门到精通
科研绘图与学术图表绘制:从入门到精通
175 0
|
9月前
|
数据可视化 数据挖掘 Linux
【数据分析与可视化】Seaborn中常用绘图模型讲解及实战(图文解释 附源码)
【数据分析与可视化】Seaborn中常用绘图模型讲解及实战(图文解释 附源码)
167 0
|
数据采集 机器学习/深度学习 SQL
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
247 0
|
机器学习/深度学习 存储 数据可视化
精心整理(含图PLUS版)|R语言生信分析,可视化(二)
精心整理(含图PLUS版)|R语言生信分析,可视化(二)
121 0
|
数据可视化 Go 数据处理
精心整理(含图PLUS版)|R语言生信分析,可视化(一)
精心整理(含图PLUS版)|R语言生信分析,可视化
219 0