<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots"> Easy Guides</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p>  </p>
<div>
<p></p>
<p>In this article, we’ll describe how to easily i) <strong>compare means</strong> of two or multiple groups; ii) and to automatically add <strong>p-values</strong> and <strong>significance levels</strong> to a ggplot (such as box plots, dot plots, bar plots and line plots …).</p>
<p><strong>Contents</strong>:</p>
<div>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#prerequisites" target="_blank">Prerequisites</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#methods-for-comparing-means" target="_blank">Methods for comparing means</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#r-functions-to-add-p-values" target="_blank">R functions to add p-values</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#compare-two-independent-groups" target="_blank">Compare two independent groups</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#compare-two-paired-samples" target="_blank">Compare two paired samples</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#compare-more-than-two-groups" target="_blank">Compare more than two groups</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#multiple-grouping-variables" target="_blank">Multiple grouping variables</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#other-plot-types" target="_blank">Other plot types</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/add-p-values-and-significance-levels-to-ggplots#infos" target="_blank">Infos</a></li>
</ul>
</div>
<div>
<h2>Prerequisites</h2>
<div>
<h3>Install and load required R packages</h3>
<p>Required R package: <a rel="nofollow" href="http://www.sthda.com/english/rpkgs/ggpubr" target="_blank">ggpubr (version >= 0.1.3)</a>, for ggplot2-based publication ready plots.</p>
<ul>
<li>Install from <a rel="nofollow" href="https://cran.r-project.org/package=ggpubr" target="_blank">CRAN</a> as follow:</li>
</ul>
<pre><code>install.packages(&quot;ggpubr&quot;)</code></pre>
<ul>
<li>Or, install the latest developmental version from <a rel="nofollow" href="https://github.com/kassambara/ggpubr" target="_blank">GitHub</a> as follow:</li>
</ul>
<pre><code>if(!require(devtools)) install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;kassambara/ggpubr&quot;)</code></pre>
<ul>
<li>Load ggpubr:</li>
</ul>
<pre><code>library(ggpubr)</code></pre>
<div>
<p>
Official documentation of ggpubr is available at: <a rel="nofollow" href="http://www.sthda.com/english/rpkgs/ggpubr" target="_blank">http://www.sthda.com/english/rpkgs/ggpubr</a>
</p>
</div>
</div>
<div>
<h3>Demo data sets</h3>
<p>Data: <a rel="nofollow" href="http://www.sthda.com/english/wiki/r-built-in-data-sets#toothgrowth" target="_blank">ToothGrowth</a> data sets.</p>
<pre><code>data(&quot;ToothGrowth&quot;)
head(ToothGrowth)</code></pre>
<pre><code>   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5</code></pre>
</div>
</div>
<div>
<h2>Methods for comparing means</h2>
<p>The standard methods to compare the means of two or more groups in R, have been largely described at: <a rel="nofollow" href="http://www.sthda.com/english/wiki/comparing-means-in-r" target="_blank">comparing means in R</a>.</p>
<p>The most common methods for comparing means include:</p>
<table>
<thead>
<tr>
<th>Methods</th>
<th>R function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>T-test</td>
<td>t.test()</td>
<td>Compare two groups (parametric)</td>
</tr>
<tr>
<td>Wilcoxon test</td>
<td>wilcox.test()</td>
<td>Compare two groups (non-parametric)</td>
</tr>
<tr>
<td>ANOVA</td>
<td>aov() or anova()</td>
<td>Compare multiple groups (parametric)</td>
</tr>
<tr>
<td>Kruskal-Wallis</td>
<td>kruskal.test()</td>
<td>Compare multiple groups (non-parametric)</td>
</tr>
</tbody>
</table>
<p>A practical guide to compute and interpret the results of each of these methods are provided at the following links:</p>
<div>
<ul>
<li>
Comparing one-sample mean to a standard known mean:
<ul>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/one-sample-t-test-in-r" target="_blank">One-Sample T-test (parametric)</a>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/one-sample-wilcoxon-signed-rank-test-in-r" target="_blank">One-Sample Wilcoxon Test (non-parametric)</a>
</li>
</ul>
</li>
<li>
Comparing the means of two independent groups:
<ul>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/unpaired-two-samples-t-test-in-r" target="_blank">Unpaired Two Samples T-test (parametric)</a>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/unpaired-two-samples-wilcoxon-test-in-r" target="_blank">Unpaired Two-Samples Wilcoxon Test (non-parametric)</a>
</li>
</ul>
</li>
<li>
Comparing the means of paired samples:
<ul>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/paired-samples-t-test-in-r" target="_blank">Paired Samples T-test (parametric)</a>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/paired-samples-wilcoxon-test-in-r" target="_blank">Paired Samples Wilcoxon Test (non-parametric)</a>
</li>
</ul>
</li>
<li>
Comparing the means of more than two groups
<ul>
<li>
Analysis of variance (ANOVA, parametric):
<ul>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/one-way-anova-test-in-r" target="_blank">One-Way ANOVA Test in R</a>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/two-way-anova-test-in-r" target="_blank">Two-Way ANOVA Test in R</a>
</li>
</ul>
</li>
<li>
<a rel="nofollow" href="http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r" target="_blank">Kruskal-Wallis Test in R (non parametric alternative to one-way ANOVA)</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div>
<h2>R functions to add p-values</h2>
<p>Here we present two new R functions in the <strong>ggpubr</strong> package:</p>
<ul>
<li><strong>compare_means</strong>(): easy to use solution to performs one and multiple mean comparisons.</li>
<li><strong>stat_compare_means</strong>(): easy to use solution to automatically add p-values and significance levels to a ggplot.</li>
</ul>
<div>
<h3>compare_means()</h3>
<p>As we’ll show in the next sections, it has multiple useful options compared to the standard R functions.</p>
<p>The simplified format is as follow:</p>
<pre><code>compare_means(formula, data, method = &quot;wilcox.test&quot;, paired = FALSE,
  group.by = NULL, ref.group = NULL, ...)</code></pre>
<div>
<ul>
<li>
<strong>formula</strong>: a formula of the form <em>x ~ group</em>, where x is a numeric variable and group is a factor with one or multiple levels. For example, <em>formula = TP53 ~ cancer_group</em>. It’s also possible to perform the test for multiple response variables at the same time. For example, <em>formula = c(TP53, PTEN) ~ cancer_group</em>.
</li>
<li>
<p>
<strong>data</strong>: a data.frame containing the variables in the formula.
</p>
</li>
<li>
<strong>method</strong>: the type of test. Default is <em>“wilcox.test”</em>. Allowed values include:
<ul>
<li>
<em>“t.test”</em> (parametric) and <em>“wilcox.test”</em>” (non-parametric). Perform comparison between two groups of samples. If the grouping variable contains more than two levels, then a pairwise comparison is performed.
</li>
<li>
<em>“anova”</em> (parametric) and <em>“kruskal.test”</em> (non-parametric). Perform one-way ANOVA test comparing multiple groups.
</li>
</ul>
</li>
<li>
<p>
<strong>paired</strong>: a logical indicating whether you want a paired test. Used only in <em>t.test</em> and in <em>wilcox.test</em>.
</p>
</li>
<li>
<p>
<strong>group.by</strong>: 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.
</p>
</li>
<li>
<p>
<strong>ref.group</strong>: 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 <em>“.all.”</em>. In this case, each of the grouping variable levels is compared to all (i.e. base-mean).
</p>
</li>
</ul>
</div>
</div>
<div>
<h3>stat_compare_means()</h3>
<p>This function extends ggplot2 for adding mean comparison p-values to a ggplot, such as box blots, dot plots, bar plots and line plots.</p>
<p>The simplified format is as follow:</p>
<pre><code>stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE,
                   label = NULL,  label.x = NULL, label.y = NULL,  ...)</code></pre>
<div>
<ul>
<li>
<p>
<strong>mapping</strong>: Set of aesthetic mappings created by aes().
</p>
</li>
<li>
<p>
<strong>comparisons</strong>: 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.
</p>
</li>
<li>
<p>
<strong>hide.ns</strong>: logical value. If TRUE, hide ns symbol when displaying significance levels.
</p>
</li>
<li>
<p>
<strong>label</strong>: character string specifying label type. Allowed values include “p.signif” (shows the significance levels), “p.format” (shows the formatted p value).
</p>
</li>
<li>
<p>
<strong>label.x,label.y</strong>: numeric values. coordinates (in data units) to be used for absolute positioning of the label. If too short they will be recycled.
</p>
</li>
<li>
<p>
<strong>…</strong>: other arguments passed to the function <strong>compare_means</strong>() such as <em>method</em>, <em>paired</em>, <em>ref.group</em>.
</p>
</li>
</ul>
</div>
</div>
</div>
<div>
<h2>Compare two independent groups</h2>
<p>Perform the test:</p>
<pre><code>compare_means(len ~ supp, data = ToothGrowth)</code></pre>
<pre><code># A tibble: 1 x 8
    .y. group1 group2          p      p.adj p.format p.signif   method
  </code></pre></div></div>