{"id":16701,"date":"2020-06-01T21:41:51","date_gmt":"2020-06-01T20:41:51","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?p=16701"},"modified":"2020-06-03T07:53:32","modified_gmt":"2020-06-03T06:53:32","slug":"how-to-add-p-values-onto-basic-ggplots","status":"publish","type":"post","link":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/","title":{"rendered":"How to Add P-Values onto Basic GGPLOTS"},"content":{"rendered":"<div id=\"rdoc\">\n<p>This article describes the basics of how to compute and <strong>add p-values to basic ggplots<\/strong> using the <em>rstatix<\/em> and the <em>ggpubr<\/em> R packages.<\/p>\n<p>You will learn how to:<\/p>\n<ul>\n<li>Perform pairwise mean comparisons and add the p-values onto basic box plots and bar plots.<\/li>\n<li>Display adjusted p-values and the significance levels onto the plots<\/li>\n<li>Format the p-value labels<\/li>\n<li>Specify manually the y position of p-value labels and shorten the width of the brackets<\/li>\n<\/ul>\n<p>We will follow the steps below for adding significance levels onto a ggplot:<\/p>\n<div class=\"block\">\n<ol style=\"list-style-type: decimal\">\n<li>\nCompute easily statistical tests (<code>t_test()<\/code> or <code>wilcox_test()<\/code>) using the <code>rstatix<\/code> package\n<\/li>\n<li>\nAuto-compute p-value label positions using the function <code>add_xy_position()<\/code> [in rstatix package].\n<\/li>\n<li>\nAdd the p-values to the plot using the function <code>stat_pvalue_manual()<\/code> [in ggpubr package]. The following key options are illustrated in some of the examples:<\/p>\n<ul>\n<li>\nThe option <code>bracket.nudge.y<\/code> is used to move up or to move down the brackets.\n<\/li>\n<li>\nThe option <code>step.increase<\/code> is used to add more space between brackets.\n<\/li>\n<li>\nThe option <code>vjust<\/code> is used to vertically adjust the position of the p-values labels\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/div>\n<p>Note that, in some situations, the p-value labels are partially hidden by the plot top border. In these cases, the ggplot2 function <code>scale_y_continuous(expand = expansion(mult = c(0, 0.1)))<\/code> can be used to <a href=\"https:\/\/www.datanovia.com\/en\/blog\/ggplot-facet-how-to-add-space-between-labels-on-the-top-of-the-chart-and-the-plot-border\/\">add more spaces between labels and the plot top border<\/a>. The option <code>mult = c(0, 0.1)<\/code> indicates that 0% and 10% spaces are respectively added at the bottom and the top of the plot.<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#prerequisites\">Prerequisites<\/a><\/li>\n<li><a href=\"#data-preparation\">Data preparation<\/a><\/li>\n<li><a href=\"#comparing-two-means\">Comparing two means<\/a>\n<ul>\n<li><a href=\"#compare-two-independent-groups\">Compare two independent groups<\/a><\/li>\n<li><a href=\"#compare-paired-samples\">Compare paired samples<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#pairwise-comparisons\">Pairwise comparisons<\/a>\n<ul>\n<li><a href=\"#create-simple-plots\">Create simple plots<\/a><\/li>\n<li><a href=\"#statistical-test\">Statistical test<\/a><\/li>\n<li><a href=\"#create-plots-with-significance-levels\">Create plots with significance levels<\/a><\/li>\n<li><a href=\"#comparisons-against-reference-groups\">Comparisons against reference groups<\/a><\/li>\n<li><a href=\"#comparisons-against-all-basemean\">Comparisons against all (basemean)<\/a><\/li>\n<li><a href=\"#comparisons-against-null-one-sample-test\">Comparisons against null: one-sample test<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"prerequisites\" class=\"section level2\">\n<h2>Prerequisites<\/h2>\n<p>Make sure you have installed the following R packages:<\/p>\n<ul>\n<li><code>ggpubr<\/code> for creating easily publication ready plots<\/li>\n<li><code>rstatix<\/code> provides pipe-friendly R functions for easy statistical analyses.<\/li>\n<\/ul>\n<p>Start by loading the following required packages:<\/p>\n<pre class=\"r\"><code>library(ggpubr)\r\nlibrary(rstatix)<\/code><\/pre>\n<\/div>\n<div id=\"data-preparation\" class=\"section level2\">\n<h2>Data preparation<\/h2>\n<pre class=\"r\"><code># Transform `dose` into factor variable\r\ndf &lt;- ToothGrowth\r\ndf$dose &lt;- as.factor(df$dose)\r\nhead(df, 3)<\/code><\/pre>\n<pre><code>##    len supp dose\r\n## 1  4.2   VC  0.5\r\n## 2 11.5   VC  0.5\r\n## 3  7.3   VC  0.5<\/code><\/pre>\n<\/div>\n<div id=\"comparing-two-means\" class=\"section level2\">\n<h2>Comparing two means<\/h2>\n<p>To compare the means of two groups, you can use either the function <code>t_test()<\/code> (parametric) or <code>wilcox_test()<\/code> (non-parametric). In the following example the t-test will be illustrated.<\/p>\n<div id=\"compare-two-independent-groups\" class=\"section level3\">\n<h3>Compare two independent groups<\/h3>\n<div id=\"box-plots-with-p-values\" class=\"section level4\">\n<h4>Box plots with p-values<\/h4>\n<pre class=\"r\"><code># Statistical test\r\nstat.test &lt;- df %&gt;%\r\n  t_test(len ~ supp) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 9\r\n##   .y.   group1 group2    n1    n2 statistic    df      p p.signif\r\n## * &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 len   OJ     VC        30    30      1.92  55.3 0.0606 ns<\/code><\/pre>\n<pre class=\"r\"><code># Box plots with p-values\r\nbxp &lt;- ggboxplot(df, x = &quot;supp&quot;, y = &quot;len&quot;, fill = &quot;#00AFBB&quot;)\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;p&quot;) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))\r\n\r\n# Customize p-value labels using glue expression \r\n# https:\/\/github.com\/tidyverse\/glue\r\nbxp + stat_pvalue_manual(\r\n  stat.test, label = &quot;T-test, p = {p}&quot;,\r\n  vjust = -1, bracket.nudge.y = 1\r\n  ) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-compare-means-two-independent-groups-box-plot-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-compare-means-two-independent-groups-box-plot-2.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"grouped-data\" class=\"section level4\">\n<h4>Grouped data<\/h4>\n<p>Group the data by the <code>dose<\/code> variable and then compare the levels of the <code>supp<\/code> variable.<\/p>\n<pre class=\"r\"><code># Statistical test\r\nstat.test &lt;- df %&gt;%\r\n  group_by(dose) %&gt;%\r\n  t_test(len ~ supp) %&gt;%\r\n  adjust_pvalue() %&gt;%\r\n  add_significance(&quot;p.adj&quot;)\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 11\r\n##   dose  .y.   group1 group2    n1    n2 statistic    df       p   p.adj p.adj.signif\r\n## * &lt;fct&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;chr&gt;       \r\n## 1 0.5   len   OJ     VC        10    10    3.17    15.0 0.00636 0.0127  *           \r\n## 2 1     len   OJ     VC        10    10    4.03    15.4 0.00104 0.00312 **          \r\n## 3 2     len   OJ     VC        10    10   -0.0461  14.0 0.964   0.964   ns<\/code><\/pre>\n<pre class=\"r\"><code># Box plots with p-values\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nbxp &lt;- ggboxplot(df, x = &quot;supp&quot;, y = &quot;len&quot;, fill = &quot;#00AFBB&quot;,\r\n                 facet.by = &quot;dose&quot;)\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;p.adj&quot;) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.10)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-grouped-data-compare-two-independent-groups-1.png\" width=\"528\" \/><\/p>\n<\/div>\n<div id=\"show-p-values-if-significant-otherwise-show-ns\" class=\"section level4\">\n<h4>Show p-values if significant otherwise show ns<\/h4>\n<p>This section describes how to display p-values when they are significant and show \u201cns\u201d when the p-values are not significant.<\/p>\n<pre class=\"r\"><code># Add a custom label column\r\n# showing adjusted p-values if significant otherwise &quot;ns&quot; \r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nstat.test$custom.label &lt;- ifelse(stat.test$p.adj &lt;= 0.05, stat.test$p.adj, &quot;ns&quot;)\r\n\r\n# Visualization\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;custom.label&quot;) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.10)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-show-significant-p-values-otherwise-ns-1.png\" width=\"528\" \/><\/p>\n<\/div>\n<div id=\"format-p-values-using-the-accuracy-option\" class=\"section level4\">\n<h4>Format p-values using the accuracy option<\/h4>\n<p>The p-values will be formatted using \u201c&lt;\u201d and \u201c&gt;\u201d.<\/p>\n<pre class=\"r\"><code>stat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nstat.test$p.format &lt;- p_format(\r\n  stat.test$p.adj, accuracy = 0.01,\r\n  leading.zero = FALSE\r\n  )\r\n# Visualization\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;p.format&quot;) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.10)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-format-p-values-using-the-accuracy-option-1.png\" width=\"528\" \/><\/p>\n<\/div>\n<div id=\"format-p-values-into-scientific-notation-format\" class=\"section level4\">\n<h4>Format p-values into scientific notation format<\/h4>\n<pre class=\"r\"><code># Format p-values into scientific format\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nstat.test$p.scient &lt;- format(stat.test$p.adj, scientific = TRUE)\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;p.scient&quot;) +\r\n   scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-p-values-in-scientific-format-1.png\" width=\"528\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"compare-paired-samples\" class=\"section level3\">\n<h3>Compare paired samples<\/h3>\n<pre class=\"r\"><code># Statistical test\r\nstat.test &lt;- df %&gt;%\r\n  t_test(len ~ supp, paired = TRUE) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 9\r\n##   .y.   group1 group2    n1    n2 statistic    df       p p.signif\r\n## * &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 len   OJ     VC        30    30      3.30    29 0.00255 **<\/code><\/pre>\n<pre class=\"r\"><code># Box plots with p-values\r\nbxp &lt;-  ggpaired(df, x = &quot;supp&quot;, y = &quot;len&quot;, fill = &quot;#E7B800&quot;,\r\n                 line.color = &quot;gray&quot;, line.size = 0.4)\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;supp&quot;)\r\nbxp + stat_pvalue_manual(stat.test, label = &quot;p.signif&quot;)\r\n\r\n# Show the p-value combined with the significance level\r\nbxp + \r\n  stat_pvalue_manual(stat.test, label = &quot;{p}{p.signif}&quot;) +\r\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.10)))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-compare-means-paired-groups-box-plot-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-compare-means-paired-groups-box-plot-2.png\" width=\"288\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"pairwise-comparisons\" class=\"section level2\">\n<h2>Pairwise comparisons<\/h2>\n<div id=\"create-simple-plots\" class=\"section level3\">\n<h3>Create simple plots<\/h3>\n<pre class=\"r\"><code># Box plots\r\nbxp &lt;- ggboxplot(df, x = &quot;dose&quot;, y = &quot;len&quot;, fill = &quot;dose&quot;, \r\n                 palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;))\r\nbxp\r\n# Bar plots showing mean +\/- SD\r\nbp &lt;- ggbarplot(df, x = &quot;dose&quot;, y = &quot;len&quot;, add = &quot;mean_sd&quot;, fill = &quot;dose&quot;, \r\n                palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;))\r\nbp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-create-simple-plots-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-create-simple-plots-2.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"statistical-test\" class=\"section level3\">\n<h3>Statistical test<\/h3>\n<p>In the following example, we\u2019ll perform T-test using the function <code>t_test()<\/code> [rstatix package]. It\u2019s also possible to use the function <code>wilcox_test()<\/code>.<\/p>\n<pre class=\"r\"><code>stat.test &lt;- df %&gt;% t_test(len ~ dose)\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 10\r\n##   .y.   group1 group2    n1    n2 statistic    df        p    p.adj p.adj.signif\r\n## * &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;       \r\n## 1 len   0.5    1         20    20     -6.48  38.0 1.27e- 7 2.54e- 7 ****        \r\n## 2 len   0.5    2         20    20    -11.8   36.9 4.40e-14 1.32e-13 ****        \r\n## 3 len   1      2         20    20     -4.90  37.1 1.91e- 5 1.91e- 5 ****<\/code><\/pre>\n<\/div>\n<div id=\"create-plots-with-significance-levels\" class=\"section level3\">\n<h3>Create plots with significance levels<\/h3>\n<pre class=\"r\"><code># Box plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;dose&quot;)\r\nbxp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;, tip.length = 0.01)\r\n\r\n# Bar plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(fun = &quot;mean_sd&quot;, x = &quot;dose&quot;)\r\nbp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;, tip.length = 0.01)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-simple-pairwise-comparisons-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-simple-pairwise-comparisons-2.png\" width=\"288\" \/><\/p>\n<p>Specify manually the y position of p-value labels and shorten the width of the brackets:<\/p>\n<pre class=\"r\"><code>bxp + \r\n  stat_pvalue_manual(\r\n    stat.test, label = &quot;p.adj.signif&quot;, tip.length = 0.01,\r\n    y.position = c(35, 40, 35), bracket.shorten = 0.05\r\n    )<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-manual-p-value-label-y-position-1.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"comparisons-against-reference-groups\" class=\"section level3\">\n<h3>Comparisons against reference groups<\/h3>\n<pre class=\"r\"><code># Statistical tests\r\nstat.test &lt;- df %&gt;% t_test(len ~ dose, ref.group = &quot;0.5&quot;) \r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 10\r\n##   .y.   group1 group2    n1    n2 statistic    df        p    p.adj p.adj.signif\r\n## * &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;       \r\n## 1 len   0.5    1         20    20     -6.48  38.0 1.27e- 7 1.27e- 7 ****        \r\n## 2 len   0.5    2         20    20    -11.8   36.9 4.40e-14 8.80e-14 ****<\/code><\/pre>\n<pre class=\"r\"><code># Box plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;dose&quot;)\r\nbxp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;, tip.length = 0.01)\r\n\r\n# Bar plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(fun = &quot;mean_sd&quot;, x = &quot;dose&quot;)\r\nbp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;, tip.length = 0.01)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-reference-groups-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-reference-groups-2.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"comparisons-against-all-basemean\" class=\"section level3\">\n<h3>Comparisons against all (basemean)<\/h3>\n<p>Each group is compared to all groups combined.<\/p>\n<pre class=\"r\"><code># Statistical tests\r\nstat.test &lt;- df %&gt;% t_test(len ~ dose, ref.group = &quot;all&quot;) \r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 10\r\n##   .y.   group1 group2    n1    n2 statistic    df           p      p.adj p.adj.signif\r\n## * &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;       &lt;dbl&gt;      &lt;dbl&gt; &lt;chr&gt;       \r\n## 1 len   all    0.5       60    20     5.82   56.4 0.000000290 0.00000087 ****        \r\n## 2 len   all    1         60    20    -0.660  57.5 0.512       0.512      ns          \r\n## 3 len   all    2         60    20    -5.61   66.5 0.000000425 0.00000087 ****<\/code><\/pre>\n<pre class=\"r\"><code># Box plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;dose&quot;) \r\nbxp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;)\r\n\r\n# Manually specify the y position\r\nbxp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;, y.position = 35)\r\n\r\n# Bar plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(fun = &quot;mean_sd&quot;, x = &quot;dose&quot;)\r\nbp + stat_pvalue_manual(stat.test, label = &quot;p.adj.signif&quot;)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-all-basemean-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-all-basemean-2.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-all-basemean-3.png\" width=\"288\" \/><\/p>\n<\/div>\n<div id=\"comparisons-against-null-one-sample-test\" class=\"section level3\">\n<h3>Comparisons against null: one-sample test<\/h3>\n<p>The one-sample test is used to compare the mean of one sample to a known standard (or theoretical \/ hypothetical) mean (<code>mu<\/code>). The default value of <code>mu<\/code> is zero.<\/p>\n<pre class=\"r\"><code># Statistical tests\r\nstat.test &lt;- df %&gt;% \r\n  group_by(dose) %&gt;%\r\n  t_test(len ~ 1) %&gt;%\r\n  adjust_pvalue() %&gt;%\r\n  add_significance(&quot;p.adj&quot;)\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 10\r\n##   dose  .y.   group1 group2         n statistic    df        p    p.adj p.adj.signif\r\n## * &lt;fct&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt;      &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;       \r\n## 1 0.5   len   1      null model    20      10.5    19 2.24e- 9 2.24e- 9 ****        \r\n## 2 1     len   1      null model    20      20.0    19 3.22e-14 6.44e-14 ****        \r\n## 3 2     len   1      null model    20      30.9    19 1.03e-17 3.09e-17 ****<\/code><\/pre>\n<pre class=\"r\"><code># Box plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(x = &quot;dose&quot;) \r\nbxp + stat_pvalue_manual(stat.test, x = &quot;dose&quot;, label = &quot;p.adj.signif&quot;)\r\n # bar plot\r\nstat.test &lt;- stat.test %&gt;% add_xy_position(fun = &quot;mean_sd&quot;, x = &quot;dose&quot;)\r\nbp + stat_pvalue_manual(stat.test, x = &quot;dose&quot;, label = &quot;p.adj.signif&quot;)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-null-1.png\" width=\"288\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggpubr\/figures\/055-add-p-values-onto-basic-ggplots-comparisons-against-null-2.png\" width=\"288\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"conclusion\" class=\"section level2\">\n<h2>Conclusion<\/h2>\n<p>This article introduces how to easily compute and add p-values onto ggplot, such as box plots and bar plots. See other related frequently questions: <a href=\"https:\/\/www.datanovia.com\/en\/blog\/tag\/ggpubr\/\">ggpubr FAQ<\/a>.<\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes the basics of how to compute and add p-values to basic ggplots using the rstatix and the ggpubr R packages. You will learn how to: Perform pairwise [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":16702,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rating_form_position":"","rating_results_position":"","mr_structured_data_type":"","footnotes":""},"categories":[341],"tags":[343],"class_list":["post-16701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-faq","tag-ggpubr"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Add P-Values onto Basic GGPLOTS - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add P-Values onto Basic GGPLOTS - Datanovia\" \/>\n<meta property=\"og:description\" content=\"This article describes the basics of how to compute and add p-values to basic ggplots using the rstatix and the ggpubr R packages. You will learn how to: Perform pairwise [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-01T20:41:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-03T06:53:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alboukadel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alboukadel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\"},\"author\":{\"name\":\"Alboukadel\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\"},\"headline\":\"How to Add P-Values onto Basic GGPLOTS\",\"datePublished\":\"2020-06-01T20:41:51+00:00\",\"dateModified\":\"2020-06-03T06:53:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\"},\"wordCount\":521,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png\",\"keywords\":[\"ggpubr\"],\"articleSection\":[\"FAQ\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\",\"name\":\"How to Add P-Values onto Basic GGPLOTS - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png\",\"datePublished\":\"2020-06-01T20:41:51+00:00\",\"dateModified\":\"2020-06-03T06:53:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png\",\"width\":960,\"height\":768,\"caption\":\"055-add-p-values-onto-basic-ggplots-logo-1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Add P-Values onto Basic GGPLOTS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e\",\"name\":\"Alboukadel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g\",\"caption\":\"Alboukadel\"},\"url\":\"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Add P-Values onto Basic GGPLOTS - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/","og_locale":"en_US","og_type":"article","og_title":"How to Add P-Values onto Basic GGPLOTS - Datanovia","og_description":"This article describes the basics of how to compute and add p-values to basic ggplots using the rstatix and the ggpubr R packages. You will learn how to: Perform pairwise [&hellip;]","og_url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/","og_site_name":"Datanovia","article_published_time":"2020-06-01T20:41:51+00:00","article_modified_time":"2020-06-03T06:53:32+00:00","og_image":[{"width":960,"height":768,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png","type":"image\/png"}],"author":"Alboukadel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Alboukadel","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#article","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/"},"author":{"name":"Alboukadel","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e"},"headline":"How to Add P-Values onto Basic GGPLOTS","datePublished":"2020-06-01T20:41:51+00:00","dateModified":"2020-06-03T06:53:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/"},"wordCount":521,"commentCount":10,"publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png","keywords":["ggpubr"],"articleSection":["FAQ"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/","url":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/","name":"How to Add P-Values onto Basic GGPLOTS - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png","datePublished":"2020-06-01T20:41:51+00:00","dateModified":"2020-06-03T06:53:32+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2020\/06\/055-add-p-values-onto-basic-ggplots-logo-1.png","width":960,"height":768,"caption":"055-add-p-values-onto-basic-ggplots-logo-1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/blog\/how-to-add-p-values-onto-basic-ggplots\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to Add P-Values onto Basic GGPLOTS"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/7767cf2bd5c91a1610c6eb53a0ff069e","name":"Alboukadel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed3108646c5c7c3d188324ab972f96ad7d9975b41b94014d7f68257791be395a?s=96&d=mm&r=g","caption":"Alboukadel"},"url":"https:\/\/www.datanovia.com\/en\/blog\/author\/kassambara\/"}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/16701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=16701"}],"version-history":[{"count":4,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/16701\/revisions"}],"predecessor-version":[{"id":16763,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/posts\/16701\/revisions\/16763"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/16702"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=16701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/categories?post=16701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/tags?post=16701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}