Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/s6juncheng/ggpval
Add statistical test or annotation to your ggplot2 plots,
https://github.com/s6juncheng/ggpval
Last synced: 3 months ago
JSON representation
Add statistical test or annotation to your ggplot2 plots,
- Host: GitHub
- URL: https://github.com/s6juncheng/ggpval
- Owner: s6juncheng
- Created: 2017-06-09T10:57:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-07-11T16:47:21.000Z (over 2 years ago)
- Last Synced: 2024-07-25T05:03:45.978Z (4 months ago)
- Language: R
- Homepage: https://s6juncheng.github.io/ggpval/
- Size: 7.82 MB
- Stars: 44
- Watchers: 4
- Forks: 9
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![CRAN checks](https://cranchecks.info/badges/summary/ggpval)](https://cran.r-project.org/web/checks/check_results_ggpval.html)
[![](http://cranlogs.r-pkg.org/badges/last-month/ggpval?color=green)](https://cran.r-project.org/package=ggpval)
[![](https://www.r-pkg.org/badges/version/ggpval?color=green)](https://cran.r-project.org/package=ggpval)
[![Dependencies](https://tinyverse.netlify.com/badge/ggpval)](https://cran.r-project.org/package=ggpval)ggpval
======`ggpval` allows you to perform statistic tests and add the corresponding
p-values to ggplots automatically. P-values can be presented numerically
or as stars (e.g. \*, \*\*). Alternatively, one can also make any text
annotation between groups.Installation
------------``` r
# Install `ggpval` from CRAN:
install.packages("ggpval")# You can install the lastest ggpval from github with:
# install.packages("devtools")
devtools::install_github("s6juncheng/ggpval")
```Example
-------Simulate data with groups.
``` r
library(ggpval)
library(data.table)
library(ggplot2)
A <- rnorm(200, 0, 3)
B <- rnorm(200, 2, 4)
G <- rep(c("G1", "G2"), each = 100)
dt <- data.table(A, B, G)
dt <- melt(dt, id.vars = "G")
theme_set(theme_classic())
```A trivial boxplot example
-------------------------Give the group pairs you want to compare in `pairs`. By default we use `wilcox.test`, you can al well use `t.test` and others. The key word arguments for the test function, such as `alternative = c("two.sided", "less", "greater")`, `paired=` can be directly given. By default, we use the save default arguments as the test function.
``` r
plt <- ggplot(dt, aes(variable, value)) +
geom_boxplot() +
geom_jitter()add_pval(plt, pairs = list(c(1, 2)), test='wilcox.test', alternative='two.sided')
```Convert with plotly with `ggplotly`
-------------------To convert the plot with `ggpval` annotation to plotly, add `plotly=TRUE`:
```r
plt <- ggplot(dt, aes(variable, value)) +
geom_boxplot() +
geom_jitter()
plt <- add_pval(plt, pairs = list(c(1, 2)), test = "t.test", plotly=TRUE)
plotly::ggplotly(plt)
```Boxplot with facets
-------------------``` r
plt <- ggplot(dt, aes(variable, value)) +
geom_boxplot() +
geom_jitter() +
facet_wrap(~G)
add_pval(plt, pairs = list(c(1, 2)))
```Bar plot
--------`ggpval` tries to infer the column which contains the data to do
statistical testing. In case this inference was wrong or not possible
(for instance the raw data column was not mapped in ggplot object), you
can specify the correct column name with `response=`.``` r
dt[, mu := mean(value),
by = c("G", "variable")]dt[, se := sd(value) / .N,
by = c("G", "variable")]plt_bar <- ggplot(dt, aes(x=variable, y=mu, fill = variable)) +
geom_bar(stat = "identity", position = 'dodge') +
geom_errorbar(aes(ymin=mu-se, ymax=mu+se),
width = .2) +
facet_wrap(~G)add_pval(plt_bar, pairs = list(c(1, 2)), response = 'value')
```Additional arguments for statistical function can also be directly
specified. Here we also the conventional "\*" format for significance
level.``` r
add_pval(plt_bar, pairs = list(c(1, 2)),
test = 't.test',
alternative = "less",
response = 'value',
pval_star = T)
```Annotate your plot with text
------------------``` r
add_pval(plt, pairs = list(c(1, 2)), annotation = "Awesome")
```In case you to want give different annotations to each facets, provide
your annotation as a list``` r
add_pval(plt, pairs = list(c(1, 2)), annotation = list("Awesome1", "Awesome2"))
```Bugs and issues
---------------Please report bugs and issues on github issue page:
https://github.com/s6juncheng/ggpval/issues.
Contributions are welcome.Acknowledgement
---------------Thanks to Vicente Yépez for testing and helping with improvement of the
package.