# Making a boxplot or violinplot for several dataframe with different number of samples

I have 3 data frames for treatment of 3 different drugs 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 MNP total
1:    LP6008337-DNA_H06  927  773 40756   0 42456
2:    LP6008334-DNA_D02 1049  799 31009   0 32857
> dim(dat1)
[1] 21  6
>

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

Patient   DEL   INS    SNP MNP  total
1:    LP6005935-DNA_F03 39168 16739  58095   0 114002
2:    LP6008269-DNA_D08   849   910 103501  11 105271

> dim(dat3)
[1] 106   6


I want to show if the number of each mutational category and total is different for each drug by boxplot, violinplot or any type of visualization

For example this picture is very nice

Can you help me?

To help us help you, please make your data available with dput() next time.

To achieve your goal with ggplot2, you would need all of your data in one data frame and in the "long format".

dat1 <- "
Patient DEL INS SNP MNP total
LP6008337-DNA_H06 927 773 40756 0 42456
LP6008334-DNA_D02 1049 799 31009 0 32857
"

dat2 <- "
Patient DEL INS SNP MNP total
LP6008031-DNA_E01 13552 3374 62105 0 79031
LP6005500-DNA_G01 539 500 43451 0 44490
"

dat3 <- "
Patient DEL INS SNP MNP total
LP6005935-DNA_F03 39168 16739 58095 0 114002
LP6008269-DNA_D08 849 910 103501 11 105271
"

# read data and add a column indicating data frame of origin (drug in this case)
dat1$$drug <- "drug1" dat2 <- readr::read_delim(dat2, delim = " ") dat2$$drug <- "drug2"
dat3\$drug <- "drug3"

# bind rows
dat <- rbind(dat1, dat2, dat3)

> dat
# A tibble: 6 x 7
Patient             DEL   INS    SNP   MNP  total drug
<chr>             <dbl> <dbl>  <dbl> <dbl>  <dbl> <chr>
1 LP6008337-DNA_H06   927   773  40756     0  42456 drug1
2 LP6008334-DNA_D02  1049   799  31009     0  32857 drug1
3 LP6008031-DNA_E01 13552  3374  62105     0  79031 drug2
4 LP6005500-DNA_G01   539   500  43451     0  44490 drug2
5 LP6005935-DNA_F03 39168 16739  58095     0 114002 drug3
6 LP6008269-DNA_D08   849   910 103501    11 105271 drug3

# convert data to long format, ggplot() expects that
dat_long <- tidyr::pivot_longer(dat, cols = 2:6)

> dat_long
# A tibble: 30 x 4
Patient           drug  name  value
<chr>             <chr> <chr> <dbl>
1 LP6008337-DNA_H06 drug1 DEL     927
2 LP6008337-DNA_H06 drug1 INS     773
3 LP6008337-DNA_H06 drug1 SNP   40756
4 LP6008337-DNA_H06 drug1 MNP       0
5 LP6008337-DNA_H06 drug1 total 42456
6 LP6008334-DNA_D02 drug1 DEL    1049

ggplot(dat_long,
aes(x = name, y = value)) +
geom_boxplot() +
facet_wrap(~drug)


EDIT: setting scales argument of facet_wrap() to "free" could be helpful

ggplot(dat_long,
aes(x = name, y = value)) +
geom_boxplot() +
facet_wrap(~drug, scales = "free")


• Sorry @haci how we show if the groups are significantly different in terms of each feature let's say the number of SNP? I have made one by geom_signif but it compares features internally rather than between groups – Exhausted Jan 22 '20 at 14:26
• @Angel, this should be a separate question but in general people use ANOVA to test the difference of three or more groups. You will have to make sure that your data meet its assumptions though. – haci Jan 22 '20 at 16:35
• Thank you by var.test function I compared them although I am not sure if t test is suitable – Exhausted Jan 22 '20 at 16:52

If you use a violin plot instead and ggplot2 to make it then:

geom_violin(...not relevant stuff..., scale="count")


will scale each violin plot according to the number of actual entries in it. I often find this nice so small groups look super skinny and people don't focus on random changes in them so much.

• Sorry what you mean by small groups look super skinny in my 3 data types? – Exhausted Jan 22 '20 at 13:11
• I didn't say anything about your groups, reread what I wrote. – Devon Ryan Jan 22 '20 at 13:12