<p>Here a way to do it is to start first by creating a dataset containing your three different group, based on the few lines you display:</p>

<pre class="lang-r prettyprint-override"><code>dat1<span class="math-container">$Condition = "dat1"
dat2$</span>Condition = "dat2"
dat3$Condition = "dat3"
colnames(dat1)[1] = "Patient"
DF &lt;- rbind(dat1,dat2,dat3)

             Patient   DEL   INS    SNP  total Condition
1: LP6008337-DNA_H06   927   773  40756  42456      dat1
2: LP6008334-DNA_D02  1049   799  31009  32857      dat1
3: LP6008031-DNA_E01 13552  3374  62105  79031      dat2
4: LP6005500-DNA_G01   539   500  43451  44490      dat2
5: LP6005935-DNA_F03 39168 16739  58095 114002      dat3
6: LP6008269-DNA_D08   849   910 103501 105271      dat3
</code></pre>

<p>Then, you can reshape <code>DF</code> in order to put all values DEL, INS, SNP and Total into a single column and create a categorical column. Doing this will be helpful for later producing the facets of your plot. To do that, you can use <code>pivot_longer</code> from <code>tidyr</code> package:</p>

<pre class="lang-r prettyprint-override"><code>library(tidyr)
DF &lt;- DF %&gt;% pivot_longer(.,cols = c(DEL,INS,SNP, total), names_to = "var", values_to = "val")

# A tibble: 24 x 4
   Patient           Condition var     val
   &lt;chr&gt;             &lt;chr&gt;     &lt;chr&gt; &lt;int&gt;
 1 LP6008337-DNA_H06 dat1      DEL     927
 2 LP6008337-DNA_H06 dat1      INS     773
 3 LP6008337-DNA_H06 dat1      SNP   40756
 4 LP6008337-DNA_H06 dat1      total 42456
 5 LP6008334-DNA_D02 dat1      DEL    1049
 6 LP6008334-DNA_D02 dat1      INS     799
 7 LP6008334-DNA_D02 dat1      SNP   31009
 8 LP6008334-DNA_D02 dat1      total 32857
 9 LP6008031-DNA_E01 dat2      DEL   13552
10 LP6008031-DNA_E01 dat2      INS    3374
# … with 14 more rows
</code></pre>

<p>Now, you can plot your data and use the function <code>stat_compare_means</code> from <code>ggpubr</code> package to get the significance display on each tab. Based on the size of your group, you may want to adjust for the type of statistical test to be used (take a look at <code>?stat_compare_means</code> to find the statistical test adequate to your dataset). You can pass various conditions to test as a list by using <code>comparisons</code> argument. </p>

<p>For facetting your graph, you can use <code>facet_grid</code> (or<code>facet_wrap</code>, try the one you preferred). Altogether, you can get the plot by writing:</p>

<pre class="lang-r prettyprint-override"><code>library(ggplot2)
library(ggpubr)
ggplot(DF, aes(x = Condition, y = val, fill = Condition))+
  geom_boxplot()+
  stat_compare_means(comparisons = list(c("dat1","dat2"), c("dat1","dat3"), c("dat2","dat3")))+
  stat_compare_means()+
  facet_grid(var~., scales = "free")
</code></pre>

<p><a href="https://i.sstatic.net/LAuWd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LAuWd.png" alt="enter image description here"></a></p>

<p>Hope it helps you to figure it out how to represent your data.</p>

CC BY-SA 4.0
2020-01-27T18:08:11.453Z
Thank you, you are amazingly helpful, how I can I do exactly the same procedure but for pairwise t-test instead of cruskal wallis? Because I found the pvalue completely different
Zizogolu
CC BY-SA 4.0
2020-01-27T18:48:17.120Z
Glad to be able to help. It's normal that Kruskal-Wallis returns different p values than pairwise.t.test because one is non-parametric and the other is parametric. I don't think you have the option to pass a `parwise.t.test` in `stat_compare_means` but you can try to add the argument `method = "t.test"` (e.g. `stat_compare_means(method = "t.test")`). But you should check that `pairwise.t.test` and `t.test` gives you the same output first. If not, you will have to add them manually using `geom_signif` or `stat_pvalue_manual`.
dc37
CC BY-SA 4.0
2020-01-27T19:13:13.587Z
Thank you, this gives error . > ggplot(DF, aes(x = Response, y = val, fill = Response))+
+   geom_boxplot()+
+   stat_compare_means(comparisons = list(c("Responders","Non_res_naive"), c("Responders","Non_res_post"), c("Non_res_naive","Non_res_post")),method = "pairwise.t.test")+
+   stat_compare_means()+
+   facet_grid(var~., scales = "free")  + scale_y_continuous(trans = "log2")
Error in .method_info(method) : 
  Non-supported method specified. Allowed methods are one of: t.test, t.test, t.test, wilcox.test, wilcox.test, wilcox.test, anova, anova, kruskal.test, kruskal.test
Zizogolu
CC BY-SA 4.0
2020-01-27T19:18:13.563Z
That's because `method = "pariwise.t.test` does not exist in `stat_compare_means`, that's why, I advised you in my comment above to use `method = "t.test"`
dc37
CC BY-SA 4.0
2020-01-27T15:36:10.237Z
2020-01-27T15:36:10.237Z
dc37
<p>I have 3 data frames for three groups of patients and in each of them I have the number of mutation types like insertion, deletion, SNP and total of mutations for each patients. In each group I have different number of patients like below</p>

<pre><code>&gt; head(dat1)
                patient  DEL  INS   SNP   total
1:    LP6008337-DNA_H06  927  773 40756   42456
2:    LP6008334-DNA_D02 1049  799 31009   32857
&gt; dim(dat1)
[1] 21  6
&gt; 

 head(dat2)
                Patient   DEL  INS   SNP total
1:    LP6008031-DNA_E01 13552 3374 62105   79031
2:    LP6005500-DNA_G01   539  500 43451   44490
&gt; dim(dat2)
[1] 33  6
&gt; 

&gt; head(dat3)
                Patient   DEL   INS    SNP    total
1:    LP6005935-DNA_F03 39168 16739  58095   114002
2:    LP6008269-DNA_D08   849   910 103501  105271

&gt; dim(dat3)
[1] 106   6
</code></pre>

<p>I want to show if the number of each mutational category and total is different between these three groups by chi-square test</p>

<p>Actual question is if total number of mutations, SNP, DEL and INS are statistically significant among groups. I used pairwise t test but I afraid this test is not a suitable test for the distribution of data moreover I don't know how to visualize the p-value</p>

<p>Imagining the comparison of total number of mutations between three groups, this picture is a good example
<a href="https://i.sstatic.net/ABR04.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ABR04.jpg" alt="enter image description here"></a></p>

<p>Or in this plot they compare several features but only two groups </p>

<p><a href="https://i.sstatic.net/Sy4cn.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Sy4cn.jpg" alt="enter image description here"></a></p>

<p>Can you help ?</p>

<p>Thanks </p>

CC BY-SA 4.0
2020-01-27T14:33:35.040Z
So make a chart with three groups. Where is the actual question?
Devon Ryan
CC BY-SA 4.0
2020-01-27T14:41:43.660Z
Actual question is if total number of mutations, SNP, DEL, INS statistically significant among groups. I used pairwise t test but I afraid this is not a suitable test moreover I don't know how to visualize the p-value
Zizogolu
<r><ggplot2>
Visualizing the difference among three groups
# Visualizing the difference among three groups

(Asked by: username_0 on 2020-01-27)

I have 3 data frames for three groups of patients and in each of them I have the number of mutation types like insertion, deletion, SNP and total of mutations for each patients. In each group I have different number of patients like below
> head(dat1)
                patient  DEL  INS   SNP   total
1:    LP6008337-DNA_H06  927  773 40756   42456
2:    LP6008334-DNA_D02 1049  799 31009   32857
> dim(dat1)
[1] 21  6
> 

 head(dat2)
                Patient   DEL  INS   SNP total
1:    LP6008031-DNA_E01 13552 3374 62105   79031
2:    LP6005500-DNA_G01   539  500 43451   44490
> dim(dat2)
[1] 33  6
> 

> head(dat3)
                Patient   DEL   INS    SNP    total
1:    LP6005935-DNA_F03 39168 16739  58095   114002
2:    LP6008269-DNA_D08   849   910 103501  105271

> dim(dat3)
[1] 106   6

I want to show if the number of each mutational category and total is different between these three groups by chi-square test
Actual question is if total number of mutations, SNP, DEL and INS are statistically significant among groups. I used pairwise t test but I afraid this test is not a suitable test for the distribution of data moreover I don't know how to visualize the p-value
Imagining the comparison of total number of mutations between three groups, this picture is a good example

Or in this plot they compare several features but only two groups 

Can you help ?
Thanks

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: So make a chart with three groups. Where is the actual question?  
> **username_0**: Actual question is if total number of mutations, SNP, DEL, INS statistically significant among groups. I used pairwise t test but I afraid this is not a suitable test moreover I don't know how to visualize the p-value  

---

## 1 answers

### Answer by username_2 on 2020-01-27. Score: 3 | Accepted answer

Here a way to do it is to start first by creating a dataset containing your three different group, based on the few lines you display:
dat1$Condition = "dat1"
dat2$Condition = "dat2"
dat3$Condition = "dat3"
colnames(dat1)[1] = "Patient"
DF <- rbind(dat1,dat2,dat3)

             Patient   DEL   INS    SNP  total Condition
1: LP6008337-DNA_H06   927   773  40756  42456      dat1
2: LP6008334-DNA_D02  1049   799  31009  32857      dat1
3: LP6008031-DNA_E01 13552  3374  62105  79031      dat2
4: LP6005500-DNA_G01   539   500  43451  44490      dat2
5: LP6005935-DNA_F03 39168 16739  58095 114002      dat3
6: LP6008269-DNA_D08   849   910 103501 105271      dat3

Then, you can reshape DF in order to put all values DEL, INS, SNP and Total into a single column and create a categorical column. Doing this will be helpful for later producing the facets of your plot. To do that, you can use pivot_longer from tidyr package:
library(tidyr)
DF <- DF %>% pivot_longer(.,cols = c(DEL,INS,SNP, total), names_to = "var", values_to = "val")

# A tibble: 24 x 4
   Patient           Condition var     val
                      
 1 LP6008337-DNA_H06 dat1      DEL     927
 2 LP6008337-DNA_H06 dat1      INS     773
 3 LP6008337-DNA_H06 dat1      SNP   40756
 4 LP6008337-DNA_H06 dat1      total 42456
 5 LP6008334-DNA_D02 dat1      DEL    1049
 6 LP6008334-DNA_D02 dat1      INS     799
 7 LP6008334-DNA_D02 dat1      SNP   31009
 8 LP6008334-DNA_D02 dat1      total 32857
 9 LP6008031-DNA_E01 dat2      DEL   13552
10 LP6008031-DNA_E01 dat2      INS    3374
# … with 14 more rows

Now, you can plot your data and use the function stat_compare_means from ggpubr package to get the significance display on each tab. Based on the size of your group, you may want to adjust for the type of statistical test to be used (take a look at ?stat_compare_means to find the statistical test adequate to your dataset). You can pass various conditions to test as a list by using comparisons argument. 
For facetting your graph, you can use facet_grid (orfacet_wrap, try the one you preferred). Altogether, you can get the plot by writing:
library(ggplot2)
library(ggpubr)
ggplot(DF, aes(x = Condition, y = val, fill = Condition))+
  geom_boxplot()+
  stat_compare_means(comparisons = list(c("dat1","dat2"), c("dat1","dat3"), c("dat2","dat3")))+
  stat_compare_means()+
  facet_grid(var~., scales = "free")


Hope it helps you to figure it out how to represent your data.

> **Comments:**
> **username_0**: Thank you, you are amazingly helpful, how I can I do exactly the same procedure but for pairwise t-test instead of cruskal wallis? Because I found the pvalue completely different  
> **username_2**: Glad to be able to help. It's normal that Kruskal-Wallis returns different p values than pairwise.t.test because one is non-parametric and the other is parametric. I don't think you have the option to pass a `parwise.t.test` in `stat_compare_means` but you can try to add the argument `method = "t.test"` (e.g. `stat_compare_means(method = "t.test")`). But you should check that `pairwise.t.test` and `t.test` gives you the same output first. If not, you will have to add them manually using `geom_signif` or `stat_pvalue_manual`.  
> **username_0**: Thank you, this gives error . > ggplot(DF, aes(x = Response, y = val, fill = Response))+ +   geom_boxplot()+ +   stat_compare_means(comparisons = list(c("Responders","Non_res_naive"), c("Responders","Non_res_post"), c("Non_res_naive","Non_res_post")),method = "pairwise.t.test")+ +   stat_compare_means()+ +   facet_grid(var~., scales = "free")  + scale_y_continuous(trans = "log2") Error in .method_info(method) :    Non-supported method specified. Allowed methods are one of: t.test, t.test, t.test, wilcox.test, wilcox.test, wilcox.test, anova, anova, kruskal.test, kruskal.test  
> **username_2**: That's because `method = "pariwise.t.test` does not exist in `stat_compare_means`, that's why, I advised you in my comment above to use `method = "t.test"`  

---

