<div>
<p><br></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 href="http://www.sthda.com/#prerequisites">Prerequisites</a></li>
<li><a href="http://www.sthda.com/#methods-for-comparing-means">Methods for comparing means</a></li>
<li><a href="http://www.sthda.com/#r-functions-to-add-p-values">R functions to add p-values</a></li>
<li><a href="http://www.sthda.com/#compare-two-independent-groups">Compare two independent groups</a></li>
<li><a href="http://www.sthda.com/#compare-two-paired-samples">Compare two paired samples</a></li>
<li><a href="http://www.sthda.com/#compare-more-than-two-groups">Compare more than two groups</a></li>
<li><a href="http://www.sthda.com/#multiple-grouping-variables">Multiple grouping variables</a></li>
<li><a href="http://www.sthda.com/#other-plot-types">Other plot types</a></li>
<li><a href="http://www.sthda.com/#infos">Infos</a></li>
</ul>
</div>
<div>
<h2>Prerequisites</h2>
<div>
<h3>Install and load required R packages</h3>
<p>Required R package: <a href="http://www.sthda.com/english/rpkgs/ggpubr">ggpubr (version >= 0.1.3)</a>, for ggplot2-based publication ready plots.</p>
<ul>
<li>Install from <a href="https://cran.r-project.org/package=ggpubr">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 href="https://github.com/kassambara/ggpubr">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 href="http://www.sthda.com/english/rpkgs/ggpubr">http://www.sthda.com/english/rpkgs/ggpubr</a>
</p>
</div>
</div>
<div>
<h3>Demo data sets</h3>
<p>Data: <a href="http://www.sthda.com/english/wiki/r-built-in-data-sets#toothgrowth">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 href="http://www.sthda.com/english/wiki/comparing-means-in-r">comparing means in R</a>.</p>
<p>The most common methods for comparing means include:</p>
<table>
<tr>
Methods
R function
Description
</tr>
<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>
</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 href="http://www.sthda.com/english/wiki/one-sample-t-test-in-r">One-Sample T-test (parametric)</a>
</li>
<li>
<a href="http://www.sthda.com/english/wiki/one-sample-wilcoxon-signed-rank-test-in-r">One-Sample Wilcoxon Test (non-parametric)</a>
</li>
</ul>
</li>
<li>
Comparing the means of two independent groups:
<ul>
<li>
<a href="http://www.sthda.com/english/wiki/unpaired-two-samples-t-test-in-r">Unpaired Two Samples T-test (parametric)</a>
</li>
<li>
<a href="http://www.sthda.com/english/wiki/unpaired-two-samples-wilcoxon-test-in-r">Unpaired Two-Samples Wilcoxon Test (non-parametric)</a>
</li>
</ul>
</li>
<li>
Comparing the means of paired samples:
<ul>
<li>
<a href="http://www.sthda.com/english/wiki/paired-samples-t-test-in-r">Paired Samples T-test (parametric)</a>
</li>
<li>
<a href="http://www.sthda.com/english/wiki/paired-samples-wilcoxon-test-in-r">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 href="http://www.sthda.com/english/wiki/one-way-anova-test-in-r">One-Way ANOVA Test in R</a>
</li>
<li>
<a href="http://www.sthda.com/english/wiki/two-way-anova-test-in-r">Two-Way ANOVA Test in R</a>
</li>
</ul>
</li>
<li>
<a href="http://www.sthda.com/english/wiki/kruskal-wallis-test-in-r">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 x ~ group, where x is a numeric variable and group is a factor with one or multiple levels. For example, formula = TP53 ~ cancer_group. It’s also possible to perform the test for multiple response variables at the same time. For example, formula = c(TP53, PTEN) ~ cancer_group.
</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 “wilcox.test”. Allowed values include:
<ul>
<li>
“t.test” (parametric) and “wilcox.test”&quot; (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>
“anova” (parametric) and “kruskal.test” (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 t.test and in wilcox.test.
</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 “.all.”. 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 method, paired, ref.group.
</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
  
1   len     OJ     VC 0.06449067 0.06449067    0.064       ns Wilcoxon</code></pre>
<div>
<p>
By default <strong>method = “wilcox.test”</strong> (non-parametric test). You can also specify <strong>method = “t.test”</strong> for a parametric t-test.
</p>
</div>
<p>Returned value is a data frame with the following columns:</p>
<ul>
<li>.y.: the y variable used in the test.</li>
<li>p: the p-value</li>
<li>p.adj: the adjusted p-value. Default value for p.adjust.method = “holm”</li>
<li>p.format: the formatted p-value</li>
<li>p.signif: the significance level.</li>
<li>method: the statistical test used to compare groups.</li>
</ul>
<p>Create a box plot with p-values:</p>
<pre><code>p </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<p>Note that, the p-value label position can be adjusted using the arguments: label.x, label.y, hjust and vjust.</p>
<p>The default p-value label displayed is obtained by concatenating the <strong>method</strong> and the <strong>p</strong> columns of the returned data frame by the function <strong>compare_means</strong>(). You can specify other combinations using the <strong>aes</strong>() function.</p>
<p>For example,</p>
<ul>
<li>
<strong>aes(label = ..p.format..)</strong> or <strong>aes(label = paste0(“p =”, ..p.format..))</strong>: display only the formatted p-value (without the method name)</li>
<li>
<strong>aes(label = ..p.signif..)</strong>: display only the significance level.</li>
<li>
<strong>aes(label = paste0(..method.., “\n”, “p =”, ..p.format..))</strong>: Use line break (“\n”) between the method name and the p-value.</li>
</ul>
<p>As an illustration, type this:</p>
<pre><code>p + stat_compare_means( aes(label = ..p.signif..), 
                        label.x = 1.5, label.y = 40)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<p>If you prefer, it’s also possible to specify the argument label as a character vector:</p>
<pre><code>p + stat_compare_means( label = &quot;p.signif&quot;, label.x = 1.5, label.y = 40)</code></pre>
</div>
<div>
<h2>Compare two paired samples</h2>
<p>Perform the test:</p>
<pre><code>compare_means(len ~ supp, data = ToothGrowth, paired = TRUE)</code></pre>
<pre><code># A tibble: 1 x 8
    .y. group1 group2           p       p.adj p.format p.signif   method
  
1   len     OJ     VC 0.004312554 0.004312554   0.0043       ** Wilcoxon</code></pre>
<p>Visualize paired data using the <strong>ggpaired</strong>() function:</p>
<pre><code>ggpaired(ToothGrowth, x = &quot;supp&quot;, y = &quot;len&quot;,
         color = &quot;supp&quot;, line.color = &quot;gray&quot;, line.size = 0.4,
         palette = &quot;jco&quot;)+
  stat_compare_means(paired = TRUE)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
</div>
<div>
<h2>Compare more than two groups</h2>
<ul>
<li>Global test:</li>
</ul>
<pre><code># Global test
compare_means(len ~ dose,  data = ToothGrowth, method = &quot;anova&quot;)</code></pre>
<pre><code># A tibble: 1 x 6
    .y.            p        p.adj p.format p.signif method
  
1   len 9.532727e-16 9.532727e-16  9.5e-16     ****  Anova</code></pre>
<p>Plot with global p-value:</p>
<pre><code># Default method = &quot;kruskal.test&quot; for multiple groups
ggboxplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;dose&quot;, palette = &quot;jco&quot;)+
  stat_compare_means()

# Change method to anova
ggboxplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;dose&quot;, palette = &quot;jco&quot;)+
  stat_compare_means(method = &quot;anova&quot;)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<ul>
<li>
<strong>Pairwise comparisons</strong>. If the grouping variable contains more than two levels, then pairwise tests will be performed automatically. The default method is “wilcox.test”. You can change this to “t.test”.</li>
</ul>
<pre><code># Perorm pairwise comparisons
compare_means(len ~ dose,  data = ToothGrowth)</code></pre>
<pre><code># A tibble: 3 x 8
    .y. group1 group2            p        p.adj p.format p.signif   method
  
1   len    0.5      1 7.020855e-06 1.404171e-05  7.0e-06     **** Wilcoxon
2   len    0.5      2 8.406447e-08 2.521934e-07  8.4e-08     **** Wilcoxon
3   len      1      2 1.772382e-04 1.772382e-04  0.00018      *** Wilcoxon</code></pre>
<pre><code># Visualize: Specify the comparisons you want
my_comparisons </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<p>If you want to specify the precise y location of bars, use the argument <strong>label.y</strong>:</p>
<pre><code>ggboxplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;dose&quot;, palette = &quot;jco&quot;)+ 
  stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))+
  stat_compare_means(label.y = 45)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<p>(Adding bars, connecting compared groups, has been facilitated by the <a href="https://github.com/Artjom-Metro/ggsignif">ggsignif</a> R package )</p>
<ul>
<li>
<strong>Multiple pairwise tests against a reference group</strong>:</li>
</ul>
<pre><code># Pairwise comparison against reference
compare_means(len ~ dose,  data = ToothGrowth, ref.group = &quot;0.5&quot;,
              method = &quot;t.test&quot;)</code></pre>
<pre><code># A tibble: 2 x 8
    .y. group1 group2            p        p.adj p.format p.signif method
  
1   len    0.5      1 6.697250e-09 6.697250e-09  6.7e-09     **** T-test
2   len    0.5      2 1.469534e-16 2.939068e-16  </code></pre>
<pre><code># Visualize
ggboxplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;dose&quot;, palette = &quot;jco&quot;)+
  stat_compare_means(method = &quot;anova&quot;, label.y = 40)+      # Add global p-value
  stat_compare_means(label = &quot;p.signif&quot;, method = &quot;t.test&quot;,
                     ref.group = &quot;0.5&quot;)                    # Pairwise comparison against reference</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<ul>
<li>
<strong>Multiple pairwise tests against all (base-mean)</strong>:</li>
</ul>
<pre><code># Comparison of each group against base-mean
compare_means(len ~ dose,  data = ToothGrowth, ref.group = &quot;.all.&quot;,
              method = &quot;t.test&quot;)</code></pre>
<pre><code># A tibble: 3 x 8
    .y. group1 group2            p        p.adj p.format p.signif method
  
1   len  .all.    0.5 1.244626e-06 3.733877e-06  1.2e-06     **** T-test
2   len  .all.      1 5.667266e-01 5.667266e-01     0.57       ns T-test
3   len  .all.      2 1.371704e-05 2.743408e-05  1.4e-05     **** T-test</code></pre>
<pre><code># Visualize
ggboxplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;,
          color = &quot;dose&quot;, palette = &quot;jco&quot;)+
  stat_compare_means(method = &quot;anova&quot;, label.y = 40)+      # Add global p-value
  stat_compare_means(label = &quot;p.signif&quot;, method = &quot;t.test&quot;,
                     ref.group = &quot;.all.&quot;)                  # Pairwise comparison against all</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<p>A typical situation, where pairwise comparisons against “all” can be useful, is illustrated here using the myeloma data set from the <a href="https://github.com/kassambara/survminer"><strong>survminer</strong></a> package.</p>
<p>We’ll plot the expression profile of the DEPDC1 gene according to the patients’ molecular groups. We want to know if there is any difference between groups. If yes, where the difference is?</p>
<p>To answer to this question, you can perform a pairwise comparison between all the 7 groups. This will lead to a lot of comparisons between all possible combinations. If you have many groups, as here, it might be difficult to interpret.</p>
<p>Another easy solution is to compare each of the seven groups against “all” (i.e. base-mean). When the test is significant, then you can conclude that DEPDC1 is significantly overexpressed or downexpressed in a group xxx compared to all.</p>
<pre><code># Load myeloma data from survminer package
if(!require(survminer)) install.packages(&quot;survminer&quot;)
data(&quot;myeloma&quot;, package = &quot;survminer&quot;)

# Perform the test
compare_means(DEPDC1 ~ molecular_group,  data = myeloma,
              ref.group = &quot;.all.&quot;, method = &quot;t.test&quot;)</code></pre>
<pre><code># A tibble: 7 x 8
     .y. group1           group2            p        p.adj p.format p.signif method
   
1 DEPDC1  .all.       Cyclin D-1 1.496896e-01 4.490687e-01  0.14969       ns T-test
2 DEPDC1  .all.       Cyclin D-2 5.231428e-01 1.000000e+00  0.52314       ns T-test
3 DEPDC1  .all.     Hyperdiploid 2.815333e-04 1.689200e-03  0.00028      *** T-test
4 DEPDC1  .all. Low bone disease 5.083985e-03 2.541992e-02  0.00508       ** T-test
5 DEPDC1  .all.              MAF 8.610664e-02 3.444265e-01  0.08611       ns T-test
6 DEPDC1  .all.            MMSET 5.762908e-01 1.000000e+00  0.57629       ns T-test
7 DEPDC1  .all.    Proliferation 1.241416e-09 8.689910e-09  1.2e-09     **** T-test</code></pre>
<pre><code># Visualize the expression profile
ggboxplot(myeloma, x = &quot;molecular_group&quot;, y = &quot;DEPDC1&quot;, color = &quot;molecular_group&quot;, 
          add = &quot;jitter&quot;, legend = &quot;none&quot;) +
  rotate_x_text(angle = 45)+
  geom_hline(yintercept = mean(myeloma$DEPDC1), linetype = 2)+ # Add horizontal line at base mean
  stat_compare_means(method = &quot;anova&quot;, label.y = 1600)+        # Add global annova p-value
  stat_compare_means(label = &quot;p.signif&quot;, method = &quot;t.test&quot;,
                     ref.group = &quot;.all.&quot;)                      # Pairwise comparison against all</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<div>
<p>
From the plot above, we can conclude that DEPDC1 is significantly overexpressed in proliferation group and, it’s significantly downexpressed in Hyperdiploid and Low bone disease compared to all.
</p>
</div>
<div>
<p>
Note that, if you want to hide the ns symbol, specify the argument hide.ns = TRUE.
</p>
</div>
<pre><code># Visualize the expression profile
ggboxplot(myeloma, x = &quot;molecular_group&quot;, y = &quot;DEPDC1&quot;, color = &quot;molecular_group&quot;, 
          add = &quot;jitter&quot;, legend = &quot;none&quot;) +
  rotate_x_text(angle = 45)+
  geom_hline(yintercept = mean(myeloma$DEPDC1), linetype = 2)+ # Add horizontal line at base mean
  stat_compare_means(method = &quot;anova&quot;, label.y = 1600)+        # Add global annova p-value
  stat_compare_means(label = &quot;p.signif&quot;, method = &quot;t.test&quot;,
                     ref.group = &quot;.all.&quot;, hide.ns = TRUE)      # Pairwise comparison against all</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
</div>
<div>
<h2>Multiple grouping variables</h2>
<ul>
<li>
<strong>Two independent sample comparisons after grouping the data by another variable</strong>:</li>
</ul>
<p>Perform the test:</p>
<pre><code>compare_means(len ~ supp, data = ToothGrowth, 
              group.by = &quot;dose&quot;)</code></pre>
<pre><code># A tibble: 3 x 9
   dose   .y. group1 group2           p      p.adj p.format p.signif   method
  
1   0.5   len     OJ     VC 0.023186427 0.04637285    0.023        * Wilcoxon
2   1.0   len     OJ     VC 0.004030367 0.01209110    0.004       ** Wilcoxon
3   2.0   len     OJ     VC 1.000000000 1.00000000    1.000       ns Wilcoxon</code></pre>
<div>
<p>
In the example above, for each level of the variable “dose”, we compare the means of the variable “len” in the different groups formed by the grouping variable “supp”.
</p>
</div>
<p>Visualize (1/2). Create a multi-panel box plots facetted by group (here, “dose”):</p>
<pre><code># Box plot facetted by &quot;dose&quot;
p </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<pre><code># Or use significance symbol as label
p + stat_compare_means(label =  &quot;p.signif&quot;, label.x = 1.5)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<div>
<p>
To hide the ‘ns’ symbol, use the argument <strong>hide.ns = TRUE</strong>.
</p>
</div>
<p>Visualize (2/2). Create one single panel with all box plots. Plot y = “len” by x = “dose” and color by “supp”:</p>
<pre><code>p </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<pre><code># Show only p-value
p + stat_compare_means(aes(group = supp), label = &quot;p.format&quot;)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<pre><code># Use significance symbol as label
p + stat_compare_means(aes(group = supp), label = &quot;p.signif&quot;)</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<ul>
<li>
<strong>Paired sample comparisons after grouping the data by another variable</strong>:</li>
</ul>
<p>Perform the test:</p>
<pre><code>compare_means(len ~ supp, data = ToothGrowth, 
              group.by = &quot;dose&quot;, paired = TRUE)</code></pre>
<pre><code># A tibble: 3 x 9
   dose   .y. group1 group2          p      p.adj p.format p.signif   method
  
1   0.5   len     OJ     VC 0.03296938 0.06593876    0.033        * Wilcoxon
2   1.0   len     OJ     VC 0.01905889 0.05717667    0.019        * Wilcoxon
3   2.0   len     OJ     VC 1.00000000 1.00000000    1.000       ns Wilcoxon</code></pre>
<p>Visualize. Create a multi-panel box plots facetted by group (here, “dose”):</p>
<pre><code># Box plot facetted by &quot;dose&quot;
p </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
</div>
<div>
<h2>Other plot types</h2>
<ul>
<li>
<strong>Bar and line plots</strong> (one grouping variable):</li>
</ul>
<pre><code># Bar plot of mean +/-se
ggbarplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;, add = &quot;mean_se&quot;)+
  stat_compare_means() +                                         # Global p-value
  stat_compare_means(ref.group = &quot;0.5&quot;, label = &quot;p.signif&quot;,
                     label.y = c(22, 29))                   # compare to ref.group

# Line plot of mean +/-se
ggline(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;, add = &quot;mean_se&quot;)+
  stat_compare_means() +                                         # Global p-value
  stat_compare_means(ref.group = &quot;0.5&quot;, label = &quot;p.signif&quot;,
                     label.y = c(22, 29))     </code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
<ul>
<li>
<strong>Bar and line plots</strong> (two grouping variables):</li>
</ul>
<pre><code>ggbarplot(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;, add = &quot;mean_se&quot;,
          color = &quot;supp&quot;, palette = &quot;jco&quot;, 
          position = position_dodge(0.8))+
  stat_compare_means(aes(group = supp), label = &quot;p.signif&quot;, label.y = 29)

ggline(ToothGrowth, x = &quot;dose&quot;, y = &quot;len&quot;, add = &quot;mean_se&quot;,
          color = &quot;supp&quot;, palette = &quot;jco&quot;)+
  stat_compare_means(aes(group = supp), label = &quot;p.signif&quot;, 
                     label.y = c(16, 25, 29))</code></pre>
<div>
<p>
Add p-values and significance levels to ggplots
</p>
</div>
</div>
<div>
<h2>Infos</h2>
<p>This analysis has been performed using <strong>R software</strong> (ver. 3.3.2) and <strong>ggpubr</strong> (ver. 0.1.3).</p>
</div>
</div>