[BioC] Generating heat map of pre-organised data in matrix without clustering using ggplot2 or heatmap.2

Jakub Stanislaw Nowak jakub.nowak at ed.ac.uk
Sun Aug 10 05:40:34 CEST 2014


Hi James,

Many thanks for your email. I managed to make the heat map look better.

However I still have some minor problems with a layout.

I decided to put key above the heat map by using lmat parameter.

However when I do that key is not aligned with the heat map. I believe it is due the mar settings for the key. Also the key.ylab overlaps with ylab ticks.
I tried to call key.par setting as I think I can sort it with mar and mgp setting. However whenever I call key.par my key completely disappears. 
Even when I call default parameters for mar and mgp.

Here is the code I used, commented out is the key.par line.
```{r}
if (!require("gplots")) {
 install.packages("gplots", dependencies = TRUE)
 library(gplots)
 }
if (!require("RColorBrewer")) {
 install.packages("RColorBrewer", dependencies = TRUE)
 library(RColorBrewer)
 }
mypalette <- colorRampPalette(c("red", "black", "green"))(n = 299)
colbreaks <- c(seq(-6,-3,length=130), # for red
seq(-3,3,length=70), # for black
seq(3.10,length=100)) # for green
pdf("heatmap.pdf",10,15),
heatmap.2(data.heatmap.matrix,
  Colv="FALSE",
  Rowv="FALSE",
  main = "Corelation", 
  notecol="black",      
  density.info="density",  
  trace="none",
  colsep=1,rowsep=c(1:51),sepcolor="grey",sepwidth=c(0.005,0.01),
  labCol=c("lineA","LineB"),
  srtCol=0,
  cexCol = 1.5,
  offsetCol=(4),
  adjCol=c(0.48,0),
  margins =c(5,8),     
  col=mypalette,       
  breaks=colbreaks,    
  keysize=0.1,
  #key.par=list(mar=c(2, 2, 2, 1), mgp=(2,1,0)),
  key.title=NA,
  key.xlab=("Fold change"),
  key.ylab=("Density"),
  key.xtickfun=function(){
    key.breaks <- parent.frame()$breaks
    return(list(
      at=parent.frame()$scale01(c(key.breaks[1],key.breaks[length(key.breaks)])),
      labels=c(as.character(key.breaks[1]),as.character(key.breaks[length(key.breaks)]))
      ))
        },
  symkey=FALSE,
  lmat=rbind( c(3), c(0,4), c(2,1)), lwid=c(1.5,4), lhei=c(0.25,1,4), 
  )
dev.off()

```

I would be grateful if some solution can be suggested,



Thanks,

Jakub


On 8 Aug 2014, at 16:17, James W. MacDonald <jmacdon at uw.edu> wrote:

> Hi Jakub,
> 
> On Fri, Aug 8, 2014 at 7:06 AM, Jakub Stanislaw Nowak <jakub.nowak at ed.ac.uk> wrote:
> Hello bioconductor friends,
> 
> I am trying to generate heatmaps for my data and run in following problems. I would be grateful for some suggestions about heat map code.
> 
> I arranged matrix for the heatmap by myself and I would like to keep this not changed during heat map plotting. I tried both heatmap.2 (gplot) and geom_tile (ggplot2)
> 
> 1. So when I used heatmap.2 from (gpot) it keeps my matrix sort but I have problems making the layout look nice. Below is the code I used.
> Can you suggest how to:
> a) make rows and columns separated and rows bigger so it can read easier.
> 
> Put it in a pdf that is longer than it is tall:
> 
> pdf("heatmap.pdf', 10,50)
> heatmap.2(<args>)
> dev.off()
>  
> b) is there a way for changing  x label for key so it includes min and max values for colour code.
> 
> Well you can change the x label, but I don't think that is what you are talking about. Instead I believe you are asking about the x tick labels. In which case, from ?heatmap.2:
> 
> key.xtickfun: function computing tick location and labels for the xaxis
>           of the color key. Returns a named list containing parameters
>           that can be passed to ‘axis’. See examples.
> 
> And as advertised, there is an example of how you can change things.
> 
> c) can the density plot be generated above the key
> 
> Of course! Anything _can_ be done. But that doesn't mean it will be easy to do. Right above the example that shows how to change the key axis labels is an example of how you can use the 'extrafun' argument to replace the key entirely with a scatterplot. You could write a function that generates the key with a density plot right above it, and then pass that in via the extrafun argument.
> 
> Best,
> 
> Jim
> 
>  
> 
> Preparing table for the heatmap.2 from (qplot)
> ---------------------------------------
> ```{r}
> data.heatmap <- data.sorted.merged[,-c(2:5,8),drop=FALSE]
> rownames(data.heatmap)<-data.heatmap$ID
> data.heatmap <- data.heatmap[,-1]
> data.heatmap.matrix <- data.matrix(data.heatmap)
> ```
> Generating heatmap with heatmap.2
> ------------------------------
> ```{r}
> if (!require("gplots")) {
>  install.packages("gplots", dependencies = TRUE)
>  library(gplots)
>  }
> if (!require("RColorBrewer")) {
>  install.packages("RColorBrewer", dependencies = TRUE)
>  library(RColorBrewer)
>  }
> mypalette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
> #subseting default gradient with custom one for better visualisation
> colbreaks <- c(seq(-6,-3,length=110), # for red
> seq(-3,3,length=90), # for yellow
> seq(3.10,length=100)) # for green
> heatmap.2(data.heatmap.matrix,
>   main = "Correlation", # heat map title
>   notecol="black",      # change font color of cell labels to black
>   density.info="density",  # turns density plot inside color legend
>   trace="none",# turns off trace lines inside the heat map
>   scale=c("none"),
>   colsep, rowsep, sepcolor="white", sepwidth=c(1,1), #doesn't really show any separation
>   margins =c(1,15),     # widens margins around plot doesn't do much
>   col=mypalette,       # use on color palette defined earlier
>   breaks=colbreaks,    # enable color transition at specified limits
>   Rowv="NA",     # only draw a row dendrogram
>   Colv="NA",    # turn off column clustering
>   key.title=NA, # no title
>   symkey=FALSE,
> 
>   )
> ```
> 
> 2. I also tried to use geom_tile from (ggplot2) to generate this heat map. Here layout looks better but I am loosing my organisation of the matrix. Below is a code I used to make a plot
> Can you suggest:
> 1. how to switch off clustering in ggplot2
> 
> Generating heatmap with ggplot2
> 
> ```{r}
> if (!require("reshape2")) {
>  install.packages("reshape2", dependencies = TRUE)
>  library(reshape2)
>  }
> if (!require("plyr")) {
>  install.packages("plyr", dependencies = TRUE)
>  library(plyr)
>  }
> if (!require("scales")) {
>  install.packages("scales", dependencies = TRUE)
>  library(scales)
>  }
> if (!require("ggplot2")) {
>  install.packages("ggplot2", dependencies = TRUE)
>  library(ggplot2)
>  }
> data.sorted.merged.test <- data.sorted.merged[,-c(2:5,8)]
> data.heatmap2 <- melt(data.sorted.merged.test)
> data.heatmap2 <- ddply(data.heatmap2, .(variable), transform,rescale = rescale(value))
> ggplot(data.heatmap2, aes(variable, ID))+ geom_tile(aes(fill=rescale), colour="white") +
>   scale_fill_gradient(low="white", high="red", breaks=colbreaks)
> ```
> 
> Also I can attach png files of the heat map if that can help.
> 
> Thanks very much for your help,
> 
> Jakub
> 
> 
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
> 
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at r-project.org
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
> 
> 
> 
> -- 
> James W. MacDonald, M.S.
> Biostatistician
> University of Washington
> Environmental and Occupational Health Sciences
> 4225 Roosevelt Way NE, # 100
> Seattle WA 98105-6099

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
URL: <https://stat.ethz.ch/pipermail/bioconductor/attachments/20140810/7c2d6e88/attachment.pl>


More information about the Bioconductor mailing list