<p>If you use a violin plot instead and ggplot2 to make it then:</p>

<pre><code>geom_violin(...not relevant stuff..., scale="count")
</code></pre>

<p>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.</p>

CC BY-SA 4.0
2020-01-22T13:11:54.937Z
Sorry what you mean by small groups look super skinny in my 3 data types?
Zizogolu
CC BY-SA 4.0
2020-01-22T13:12:39.583Z
I didn't say anything about your groups, reread what I wrote.
Devon Ryan
CC BY-SA 4.0
2020-01-22T12:47:52.560Z
2020-01-22T12:47:52.560Z
Devon Ryan
<p>To help us help you, please make your data available with <code>dput()</code> next time.</p>

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

<pre><code>dat1 &lt;- "
Patient DEL INS SNP MNP total
LP6008337-DNA_H06 927 773 40756 0 42456
LP6008334-DNA_D02 1049 799 31009 0 32857
"

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

dat3 &lt;- "
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 &lt;- readr::read_delim(dat1, delim = " ")
dat1<span class="math-container">$drug &lt;- "drug1"
dat2 &lt;- readr::read_delim(dat2, delim = " ")
dat2$</span>drug &lt;- "drug2"
dat3 &lt;- readr::read_delim(dat3, delim = " ")
dat3$drug &lt;- "drug3"

# bind rows
dat &lt;- rbind(dat1, dat2, dat3)

&gt; dat
# A tibble: 6 x 7
  Patient             DEL   INS    SNP   MNP  total drug 
  &lt;chr&gt;             &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt;
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 &lt;- tidyr::pivot_longer(dat, cols = 2:6)

&gt; dat_long
# A tibble: 30 x 4
   Patient           drug  name  value
   &lt;chr&gt;             &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
 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)
</code></pre>

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

<p>EDIT: setting <code>scales</code> argument of <code>facet_wrap()</code> to "free" could be helpful</p>

<pre><code>ggplot(dat_long,
       aes(x = name, y = value)) +
  geom_boxplot() +
  facet_wrap(~drug, scales = "free")
</code></pre>

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

CC BY-SA 4.0
2020-01-22T14:26:45.820Z
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
Zizogolu
CC BY-SA 4.0
2020-01-22T16:35:00.897Z
@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
CC BY-SA 4.0
2020-01-22T16:52:18.853Z
Thank you by var.test function I compared them although I am not sure if t test is suitable
Zizogolu
CC BY-SA 4.0
2020-01-22T13:13:08.300Z
2020-01-23T11:09:04.033Z
2020-01-23T11:09:04.033Z
haci
<p>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</p>

<pre><code>&gt; 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
&gt; dim(dat1)
[1] 21  6
&gt; 

 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
&gt; dim(dat2)
[1] 33  6
&gt; 

&gt; 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

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

<p>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</p>

<p>For example this picture is very nice <a href="https://i.sstatic.net/Kss8C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Kss8C.png" alt="enter image description here"></a></p>

<p>Can you help me?</p>

<r><visualization><ggplot2><wgs>
Making a boxplot or violinplot for several dataframe with different number of samples
# Making a boxplot or violinplot for several dataframe with different number of samples

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

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 
Can you help me?

**Tags:** r, visualization, ggplot2, wgs

---

## 2 answers

### Answer by username_2 on 2020-01-22. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: 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  
> **username_2**: @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.  
> **username_0**: Thank you by var.test function I compared them although I am not sure if t test is suitable  

---

### Answer by username_1 on 2020-01-22. Score: 1

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.

> **Comments:**
> **username_0**: Sorry what you mean by small groups look super skinny in my 3 data types?  
> **username_1**: I didn't say anything about your groups, reread what I wrote.  

---

