Search code examples
rheatmapgplots

how to adjust the position of color legends in heatmap.2


I am trying to do an unbiased Hierarchical clustering with the following code

col.cell <- c("purple","orange", "green", "blue")[sampleinf$subtype]
heatmap.2(as.matrix(hm3),col=rev(morecols(50)),trace="none", main="Top 500 most variable genes across samples",ColSideColors=col.cell,scale="row")  
    legend("topright",      
    legend = unique(sampleinf$subtype),
    col = col.cell,     
    lty= 1.5,   lwd = 2,           
    cex=.6)

How can i make the color legend more visible. Now its superimposing with the dendrogram.

enter image description here


Solution

  • To plot outside the range of 0 to 1, you need to use par(xpd=TRUE)

     legend(x = #,y= #, xpd = TRUE, legend("topright",      
        legend = unique(sampleinf$subtype),
        col = col.cell,     
        lty= 1.5,   lwd = 2,           
        cex=.6)