# Attempting to generate a heat map like in this figure [closed]

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

• What have you tried? You could use geom_rect after setting x and y aes to Sample and Gene. Apr 4 '20 at 22:04
• There is multiple examples of how to do heatmap with ggplot2 on SO, for example, you can start by taking a look here: stackoverflow.com/questions/8406394/… or stackoverflow.com/questions/14290364/… or stackoverflow.com/questions/60162198/… ... Try to reproduce those examples and then you will know how to deal with your data
– dc37
Apr 5 '20 at 4:21

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 <- 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:

• 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 Apr 8 '20 at 9:11