# Visualizing the difference among three groups

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
>

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
>

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

• So make a chart with three groups. Where is the actual question? – Devon Ryan Jan 27 '20 at 14:33
• 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 – Exhausted Jan 27 '20 at 14:41

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
<chr>             <chr>     <chr> <int>
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.

• 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 – Exhausted Jan 27 '20 at 18:08
• 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 Jan 27 '20 at 18:48
• 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 – Exhausted Jan 27 '20 at 19:13
• 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 Jan 27 '20 at 19:18