<p>I can't reproduce your graph, because you don't make <code>muts</code> available, but it appears your problem is having <code>x = PW</code> in your ggplot call. Don't you want to that to be whatever your variable name for sample is (ie, the rownames in your <code>but</code> dataframe you have at the beginning). </p>

CC BY-SA 4.0
2020-04-20T05:43:19.743Z
Sorry I have edited my post, know you can reproduce my plot because muts is available now
Zizogolu
CC BY-SA 4.0
2020-04-20T03:47:06.113Z
2020-04-20T03:47:06.113Z
Jeff Kimbrel
<p>If you had the classification into oncogene/TSG for each gene in a column (let's call it <code>gene_class</code>) in <code>lmuts</code>, you could split both groups into facets within the plot by adding:</p>

<pre><code>+ facet_wrap(~gene_class, ncol=1)
</code></pre>

CC BY-SA 4.0
2020-04-20T05:05:44.477Z
2020-04-20T05:05:44.477Z
mrhd
<p>I have a boolean matrix of mutated or non mutated genes across some samples</p>

<pre><code>&gt; head(muts)
                  TP53 CDKN2A TSHZ3 ARID1A EPHA3 NOTCH1 ACVR2A ARID1B ARID2
LP6008336-DNA_G01    1      1     1      0     0      0      0      0     0
LP6008460-DNA_D01    1      1     0      1     0      0      0      0     0
LP6005500-DNA_D03    1      1     0      0     0      0      0      0     0
LP6008334-DNA_B02    1      0     1      0     0      0      0      0     0
LP6008334-DNA_A04    1      0     1      0     0      0      0      0     0
LP6005334-DNA_H01    1      0     0      0     1      0      0      1     0
</code></pre>

<p>By this code</p>

<pre><code>pw_rt = data.frame(
  gene = c('PIK3CA', 'EGFR', 'ERBB2', 'PTEN'),
  pw = c('RTK'),
  stringsAsFactors = F
)

pw_cc = data.frame(
  gene = c('CDKN2A', 'CCNE1'),
  pw = c('Cell Cycle'),
  stringsAsFactors = F
)

pw = rbind(pw_rt, pw_cc)

pw_col = RColorBrewer::brewer.pal(n = 3, name = 'Set1')[1:2]
names(pw_col) = unique(pw$pw)

require(dplyr)

lmuts = reshape2::melt(muts) %&gt;% as_tibble() 
colnames(lmuts) = c('sample', 'gene', 'value')

pw_n = pw<span class="math-container">$pw
names(pw_n) = pw$</span>gene

Np = nrow(muts)

lmuts %&gt;% 
  mutate(gene = paste(gene), PW = pw_n[gene]) %&gt;%
  group_by(gene, PW) %&gt;%
  summarise(N = sum(value == 1)) %&gt;%
  ungroup() %&gt;%
  mutate(
    PW = ifelse(is.na(PW), "None", PW), 
    N = paste0(N, ' (', round(N/Np * 100, 1), '%)')
    ) %&gt;%
  ggplot(aes(x = PW, y = gene, fill = PW)) +
  geom_tile() +
  geom_text(aes(label = N)) +
  theme_light() +
  theme(legend.position = 'bottom') +
  guides(fill = guide_legend('Pathway')) +
  scale_fill_manual(values = c(pw_col, `None` = 'gainsboro')) +
  scale_x_discrete(limits = c(names(pw_col), 'None')) +
  labs(
    x = "",
    y = 'Gene',
    title = "Occurrence of mutations"
  )
</code></pre>

<p>I am able to get proportion of altered pathway by these genes like </p>

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

<p>However I want to highlight oncogenes and tumour suppressors within this plot </p>

<p>For instance TP53 is a tumour suppressor and CCNE1 is an oncogene</p>

<p>I don't know how to manipulate the above code to highlight or partition genes to tumour suppressor or oncogene</p>

<p>Can you help?</p>

<p>EDITED</p>

<p>I have add extra column to lmuts like</p>

<pre><code>&gt; head(lmuts)
             sample gene value gene_class
1 LP6005690-DNA_H02 TP53     1        TSG
2 LP2000333-DNA_A01 TP53     1        TSG
3 LP6005409-DNA_D03 TP53     1        TSG
4 LP6008141-DNA_H02 TP53     1        TSG
5 LP6008336-DNA_E02 TP53     1        TSG
6 LP6008269-DNA_B06 TP53     1        TSG
&gt; 
</code></pre>

<p>but </p>

<pre><code>&gt; lmuts %&gt;% 
+     mutate(gene = paste(gene), PW = pw_n[gene]) %&gt;%
+     group_by(gene, PW) %&gt;%
+     summarise(N = sum(value == 1)) %&gt;%
+     ungroup() %&gt;%
+     mutate(
+         PW = ifelse(is.na(PW), "None", PW), 
+         N = paste0(N, ' (', round(N/Np * 100, 1), '%)')
+     ) %&gt;%
+     ggplot(aes(x = PW, y = gene, fill = PW)) +
+     geom_tile() +
+     geom_text(aes(label = N)) +
+     theme_light() +
+     theme(legend.position = 'bottom') +
+     guides(fill = guide_legend('Pathway')) +
+     scale_fill_manual(values = c(pw_col, `None` = 'gainsboro')) +
+     scale_x_discrete(limits = c(names(pw_col), 'None')) +
+     labs(
+         x = "",
+         y = 'Gene',
+         title = "Occurrence of mutations"
+     )+facet_wrap(~gene_class, ncol=1)
Error: At least one layer must contain all faceting variables: `gene_class`.
* Plot is missing `gene_class`
* Layer 1 is missing `gene_class`
* Layer 2 is missing `gene_class`
Run `rlang::last_error()` to see where the error occurred.
</code></pre>

<r><ggplot2>
Grouping a set of genes in a ggplot object
# Grouping a set of genes in a ggplot object

(Asked by: username_0 on 2020-04-19)

I have a boolean matrix of mutated or non mutated genes across some samples
> head(muts)
                  TP53 CDKN2A TSHZ3 ARID1A EPHA3 NOTCH1 ACVR2A ARID1B ARID2
LP6008336-DNA_G01    1      1     1      0     0      0      0      0     0
LP6008460-DNA_D01    1      1     0      1     0      0      0      0     0
LP6005500-DNA_D03    1      1     0      0     0      0      0      0     0
LP6008334-DNA_B02    1      0     1      0     0      0      0      0     0
LP6008334-DNA_A04    1      0     1      0     0      0      0      0     0
LP6005334-DNA_H01    1      0     0      0     1      0      0      1     0

By this code
pw_rt = data.frame(
  gene = c('PIK3CA', 'EGFR', 'ERBB2', 'PTEN'),
  pw = c('RTK'),
  stringsAsFactors = F
)

pw_cc = data.frame(
  gene = c('CDKN2A', 'CCNE1'),
  pw = c('Cell Cycle'),
  stringsAsFactors = F
)

pw = rbind(pw_rt, pw_cc)

pw_col = RColorBrewer::brewer.pal(n = 3, name = 'Set1')[1:2]
names(pw_col) = unique(pw$pw)

require(dplyr)

lmuts = reshape2::melt(muts) %>% as_tibble() 
colnames(lmuts) = c('sample', 'gene', 'value')

pw_n = pw$pw
names(pw_n) = pw$gene

Np = nrow(muts)

lmuts %>% 
  mutate(gene = paste(gene), PW = pw_n[gene]) %>%
  group_by(gene, PW) %>%
  summarise(N = sum(value == 1)) %>%
  ungroup() %>%
  mutate(
    PW = ifelse(is.na(PW), "None", PW), 
    N = paste0(N, ' (', round(N/Np * 100, 1), '%)')
    ) %>%
  ggplot(aes(x = PW, y = gene, fill = PW)) +
  geom_tile() +
  geom_text(aes(label = N)) +
  theme_light() +
  theme(legend.position = 'bottom') +
  guides(fill = guide_legend('Pathway')) +
  scale_fill_manual(values = c(pw_col, `None` = 'gainsboro')) +
  scale_x_discrete(limits = c(names(pw_col), 'None')) +
  labs(
    x = "",
    y = 'Gene',
    title = "Occurrence of mutations"
  )

I am able to get proportion of altered pathway by these genes like 

However I want to highlight oncogenes and tumour suppressors within this plot 
For instance TP53 is a tumour suppressor and CCNE1 is an oncogene
I don't know how to manipulate the above code to highlight or partition genes to tumour suppressor or oncogene
Can you help?
EDITED
I have add extra column to lmuts like
> head(lmuts)
             sample gene value gene_class
1 LP6005690-DNA_H02 TP53     1        TSG
2 LP2000333-DNA_A01 TP53     1        TSG
3 LP6005409-DNA_D03 TP53     1        TSG
4 LP6008141-DNA_H02 TP53     1        TSG
5 LP6008336-DNA_E02 TP53     1        TSG
6 LP6008269-DNA_B06 TP53     1        TSG
> 

but 
> lmuts %>% 
+     mutate(gene = paste(gene), PW = pw_n[gene]) %>%
+     group_by(gene, PW) %>%
+     summarise(N = sum(value == 1)) %>%
+     ungroup() %>%
+     mutate(
+         PW = ifelse(is.na(PW), "None", PW), 
+         N = paste0(N, ' (', round(N/Np * 100, 1), '%)')
+     ) %>%
+     ggplot(aes(x = PW, y = gene, fill = PW)) +
+     geom_tile() +
+     geom_text(aes(label = N)) +
+     theme_light() +
+     theme(legend.position = 'bottom') +
+     guides(fill = guide_legend('Pathway')) +
+     scale_fill_manual(values = c(pw_col, `None` = 'gainsboro')) +
+     scale_x_discrete(limits = c(names(pw_col), 'None')) +
+     labs(
+         x = "",
+         y = 'Gene',
+         title = "Occurrence of mutations"
+     )+facet_wrap(~gene_class, ncol=1)
Error: At least one layer must contain all faceting variables: `gene_class`.
* Plot is missing `gene_class`
* Layer 1 is missing `gene_class`
* Layer 2 is missing `gene_class`
Run `rlang::last_error()` to see where the error occurred.

**Tags:** r, ggplot2

---

## 2 answers

### Answer by username_2 on 2020-04-20. Score: 3 | Accepted answer

If you had the classification into oncogene/TSG for each gene in a column (let's call it gene_class) in lmuts, you could split both groups into facets within the plot by adding:
+ facet_wrap(~gene_class, ncol=1)


---

### Answer by username_1 on 2020-04-20. Score: 0

I can't reproduce your graph, because you don't make `muts` available, but it appears your problem is having `x = PW` in your ggplot call. Don't you want to that to be whatever your variable name for sample is (ie, the rownames in your `but` dataframe you have at the beginning).

> **Comments:**
> **username_0**: Sorry I have edited my post, know you can reproduce my plot because muts is available now  

---

