<p>Given the lack of effort from the OP, probably I shouldn't have wasted my time on this question with a lot of problems:</p>

<p>i) The question is not reproducible, the OP should have supplied their data with <code>dput()</code>.</p>

<p>ii) The data as pasted above does not quite make sense to me, why would you have three readings from the same gene and even in such a case how should these replicates be represented in a heatmap?</p>

<p>iii) The question is not specific enough and should not go like "how can I generate a heatmap" and should be more like "in an attempt to generate a heatmap like in this figure I have tried this piece of code but ..."</p>

<p>Having said these, and after downvoting the question, here is a solution leveraging the <code>ComplexHeatmap</code> package:</p>

<pre><code>library(data.table)
library(ComplexHeatmap)

my_data &lt;-
"
Sample  Gene    CCF
Sample1 POLQ    1
Sample1 CDKN2A  1
Sample1 TP53    1
Sample1 CCDC102B    0.906729075
Sample1 CCDC102B    1
Sample1 CCDC102B    1
Sample1 CCDC102B    1
Sample2 TSHZ3   1
Sample2 PIK3CA  1
Sample2 SLIT2   1
Sample2 SLIT2   1
"

my_data &lt;- fread(my_data)

my_data &lt;- dcast.data.table(my_data, 
                            formula = Gene ~ Sample,
                            value.var = "CCF",
                            fun=mean)

# dcast generates NA values as the provided data does not have values for each gene
my_data[is.na(my_data)] &lt;- 0

# Heatmap() expects a matrix input
my_matrix &lt;- as.matrix(my_data[,c("Sample1", "Sample2")])
rownames(my_matrix) &lt;- my_data$Gene

Heatmap(my_matrix,
        cell_fun = function(j, i, x, y, width, height, fill) {
          grid.text(my_matrix[i, j], x, y, gp = gpar(fontsize = 10))})
</code></pre>

<p>This is how the data look after <code>dcast</code>, in which multiple entries per gene (my point ii above) is summarized with <code>mean()</code>:</p>

<pre><code>&gt; head(my_matrix)
           Sample1 Sample2
CCDC102B 0.9766823       0
CDKN2A   1.0000000       0
PIK3CA   0.0000000       1
POLQ     1.0000000       0
SLIT2    0.0000000       1
TP53     1.0000000       0
</code></pre>

<p>Here is the resulting heatmap:</p>

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

CC BY-SA 4.0
2020-04-08T09:11:41.537Z
Sorry please don't close my question, I have edited that and I am certain I know what I want and what is the final goal
Zizogolu
CC BY-SA 4.0
2020-04-07T08:47:05.910Z
2020-04-07T08:47:05.910Z
haci
<p>I have a data frame</p>

<pre><code>Response TO CHEMOTHERAPY    Gene    CCF
Responders                  POLQ    1
Responders                 CDKN2A   1
Responders                  TP53    1
Responders              CCDC102B    0.906729075
Non-responders           CCDC102B   1
Non-responders           CCDC102B   1
Non-responders           CCDC102B   1
Non-responders             TSHZ3    1
Non-responders             PIK3CA   1
Responders                 SLIT2    1
Non-responders              SLIT2   1
</code></pre>

<p>I have <code>two groups of patients</code>; <code>Responders to chemotherapy and non-responders</code>. For each group I have calculated <code>cancer cell faction (CCF)</code> for a fixed set of genes so that an individual gene has several reads in each group</p>

<p>If <code>CCF = 1</code> the gene clonal and if CCF &lt; 1 gene is sub clonal, for a given gene for each group, I therefore would have the number of clonal and sub clonal for this gene, for example 2 clonal 7 sub clonal which would be 2/7 something like the below heat map</p>

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

<p>By your kindly code I produced this</p>

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

<p>Which is not well informative</p>

<p>This is another potential example heat map </p>

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

<p>Which show the clonal frequency for each group</p>

<p>By the way thanks for your kindness </p>

<p>I don't know how to use <code>dput()</code> but this is my complete data</p>

<p><a href="https://www.dropbox.com/s/ol7m4nlkuyuy85a/c1.txt?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/ol7m4nlkuyuy85a/c1.txt?dl=0</a></p>

CC BY-SA 4.0
2020-04-04T22:04:13.410Z
What have you tried? You could use `geom_rect` after setting `x` and `y` `aes` to `Sample` and `Gene`.
Ram RS
CC BY-SA 4.0
2020-04-05T04:21:06.567Z
There is multiple examples of how to do heatmap with `ggplot2` on SO, for example, you can start by taking a look here: https://stackoverflow.com/questions/8406394/how-to-produce-a-heatmap-with-ggplot2/8406774#8406774 or https://stackoverflow.com/questions/14290364/heatmap-with-values-ggplot2 or https://stackoverflow.com/questions/60162198/customizing-heatmap-appearance-using-ggplot-in-r/60163171#60163171 ... Try to reproduce those examples and then you will know how to deal with your data
dc37
<r><ggplot2>
Attempting to generate a heat map like in this figure
# Attempting to generate a heat map like in this figure

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

I have a data frame
Response TO CHEMOTHERAPY    Gene    CCF
Responders                  POLQ    1
Responders                 CDKN2A   1
Responders                  TP53    1
Responders              CCDC102B    0.906729075
Non-responders           CCDC102B   1
Non-responders           CCDC102B   1
Non-responders           CCDC102B   1
Non-responders             TSHZ3    1
Non-responders             PIK3CA   1
Responders                 SLIT2    1
Non-responders              SLIT2   1

I have two groups of patients; Responders to chemotherapy and non-responders. For each group I have calculated cancer cell faction (CCF) for a fixed set of genes so that an individual gene has several reads in each group
If CCF = 1 the gene clonal and if CCF < 1 gene is sub clonal, for a given gene for each group, I therefore would have the number of clonal and sub clonal for this gene, for example 2 clonal 7 sub clonal which would be 2/7 something like the below heat map

By your kindly code I produced this

Which is not well informative
This is another potential example heat map 

Which show the clonal frequency for each group
By the way thanks for your kindness 
I don't know how to use dput() but this is my complete data
https://www.dropbox.com/s/ol7m4nlkuyuy85a/c1.txt?dl=0

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: What have you tried? You could use `geom_rect` after setting `x` and `y` `aes` to `Sample` and `Gene`.  
> **username_2**: There is multiple examples of how to do heatmap with `ggplot2` on SO, for example, you can start by taking a look here: https://stackoverflow.com/questions/8406394/how-to-produce-a-heatmap-with-ggplot2/8406774#8406774 or https://stackoverflow.com/questions/14290364/heatmap-with-values-ggplot2 or https://stackoverflow.com/questions/60162198/customizing-heatmap-appearance-using-ggplot-in-r/60163171#60163171 ... Try to reproduce those examples and then you will know how to deal with your data  

---

## 1 answers

### Answer by username_3 on 2020-04-07. Score: 1

Given the lack of effort from the OP, probably I shouldn't have wasted my time on this question with a lot of problems:
i) The question is not reproducible, the OP should have supplied their data with dput().
ii) The data as pasted above does not quite make sense to me, why would you have three readings from the same gene and even in such a case how should these replicates be represented in a heatmap?
iii) The question is not specific enough and should not go like "how can I generate a heatmap" and should be more like "in an attempt to generate a heatmap like in this figure I have tried this piece of code but ..."
Having said these, and after downvoting the question, here is a solution leveraging the ComplexHeatmap package:
library(data.table)
library(ComplexHeatmap)

my_data <-
"
Sample  Gene    CCF
Sample1 POLQ    1
Sample1 CDKN2A  1
Sample1 TP53    1
Sample1 CCDC102B    0.906729075
Sample1 CCDC102B    1
Sample1 CCDC102B    1
Sample1 CCDC102B    1
Sample2 TSHZ3   1
Sample2 PIK3CA  1
Sample2 SLIT2   1
Sample2 SLIT2   1
"

my_data <- fread(my_data)

my_data <- dcast.data.table(my_data, 
                            formula = Gene ~ Sample,
                            value.var = "CCF",
                            fun=mean)

# dcast generates NA values as the provided data does not have values for each gene
my_data[is.na(my_data)] <- 0

# Heatmap() expects a matrix input
my_matrix <- as.matrix(my_data[,c("Sample1", "Sample2")])
rownames(my_matrix) <- my_data$Gene

Heatmap(my_matrix,
        cell_fun = function(j, i, x, y, width, height, fill) {
          grid.text(my_matrix[i, j], x, y, gp = gpar(fontsize = 10))})

This is how the data look after dcast, in which multiple entries per gene (my point ii above) is summarized with mean():
> head(my_matrix)
           Sample1 Sample2
CCDC102B 0.9766823       0
CDKN2A   1.0000000       0
PIK3CA   0.0000000       1
POLQ     1.0000000       0
SLIT2    0.0000000       1
TP53     1.0000000       0

Here is the resulting heatmap:

> **Comments:**
> **username_0**: Sorry please don't close my question, I have edited that and I am certain I know what I want and what is the final goal  

---

