-1
$\begingroup$

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
> 

 head(dat2)
                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
> 

> head(dat3)
                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 enter image description here

Can you help me?

$\endgroup$

2 Answers 2

2
$\begingroup$

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 <- readr::read_delim(dat1, delim = " ")
dat1$drug <- "drug1"
dat2 <- readr::read_delim(dat2, delim = " ")
dat2$drug <- "drug2"
dat3 <- readr::read_delim(dat3, delim = " ")
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)

enter image description here

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")

enter image description here

$\endgroup$
3
  • $\begingroup$ 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 $\endgroup$
    – Angel
    Jan 22, 2020 at 14:26
  • 1
    $\begingroup$ @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. $\endgroup$
    – haci
    Jan 22, 2020 at 16:35
  • $\begingroup$ Thank you by var.test function I compared them although I am not sure if t test is suitable $\endgroup$
    – Angel
    Jan 22, 2020 at 16:52
1
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.