This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to plot gene expression heatmap based on groups in R

I have derived a list of deferentially expressed genes and would want to plot a heatmap of the expression values and see whether there the normal and tumor samples cluster out. Below is the code:

genes <- table$index
design <- model.matrix(~ 0+factor(c(data1$Class)))
colnames(design) <- c("S", "NS")
contrast.matrix <- makeContrasts(diff=NS-S, levels=design)
fit <- lmFit(tableSub, design = design) # What should the design be?
fit2 <- contrasts.fit(fit, contrast.matrix)
options(digits=3)
fit3 <- eBayes(fit2)
writefile = topTable(fit3, number=250, genelist=genes, adjust.method = "fdr", sort.by="B")
idx = rownames(writefile)
heatmap.2(exprs(gset)[idx,],trace='none',scale='row')

Now the columns I am getting are that of my samples. The samples can be grouped into "Normal" and "Tumour". How can I display this in the heatmap?


rna-seq r

If you mean that you want a colour ('color', if you learned American English) bar, then you need to create a vector of colours that matches the columns in your gset object. This is then passed as the ColSideColors argument to heatmap.2(). For example:

colnames(gset)
'tumour1' 'tumour2' 'normal1' 'tumour3' 'normal2'

myColours <-  c('red3', 'red3', 'royalblue', 'red3', 'royalblue')

heatmap.2(..., ColSideColors = myColours, ...)

If you mean that you want to split the heatmap columns (samples) into 2 groups, then take a look at THIS section of the ComplexHeatmap vignette. Column splitting can also be done in pheatmap

enter image description here

This is what I did after reading the comments. As you can see there patches of blue within the green bar. blue represents tumor and green represents normal. Is it possible to put all the blues on the left and the greens on the right?

Check the documentation of the tool to turn of clustering of the columns.

As an aside comment: much of those normals may be from stromal cells surrounding the primary tumour, in which case they likely exhibit 'tumourigenic' properties and are not quite normal at all. If this is TCGA data, then this heatmap pattern I have seen time and time again.

1 answer

I suggest you extensively read the documentation of ComplexHeatmap at Bioconductor. It is outstandingly comprehensive with a lot of example code for all kinds of visualizations including heatmaps. Has a lot of convenience-functions such as extracting genes belonging to clusters based on hlcust etc. See https://jokergoo.github.io/ComplexHeatmap-reference/book/

Log in to answer this question.