[R] heatmap.2, adding a legend at bottom

Ding, Yuan Chun ycd|ng @end|ng |rom coh@org
Thu Oct 4 20:18:33 CEST 2018

Hi All,

I generated the attached heatmap using the following  R code and attached testfile.

I tried to move the legend to the bottom, Can anyone help me?  I have played for 3 hours and can not resolve the problem.

I realized that in the last email, I forgot to include the "Group" variable for legend. I added to the R code below.

Thank  you,


png("heatmaps_test2.png",   # create PNG for the heat map
    width = 5*300,        # 5 x 300 pixels
    height = 5*300,
    res = 300,            # 300 pixels per inch
    pointsize = 8  # smaller font size

# lmat = rbind(c(0,0,4),c(3,1,2),c(0,0,5))
# #1.      RowSideColors;2.Heatmap;3.Row tree;4.Column tree;5.Color key
# lwid = c(0.8,0.15,7) # lwid controls the column width # lhei = c(0.1,5.5,1) #lhei controls the row height

my_palette2 <- colorRampPalette(c("blue", "cyan4", "red"))(n = 299) col_breaks = c(seq(-1.5,-1.1,length=100), # for bllue
               seq(-1.099,1.099,length=100),  # for green
               seq(1.1,1.5,length=100)) # for red cols <- c("red", "black", "blue") heatmap.2(as.matrix(testfile), key=T,
          #lmat=lmat, lwid=lwid, lhei=lhei,
          symkey=FALSE,#keysize=1, key.par =list(cex=0.25),
          key.xlab="Heatmap color key",
          key.title =" ",
          RowSideColors= cols[as.numeric(as.factor(allinfo.blood$Group))],
          density.info="none",  # turns off density plot inside color legend
          trace="none",         # turns off trace lines inside the heat map
          col=my_palette2, # use color palette defined earlier
          labCol=" ", # default to colnames(x)
          #labRow=" ",
          #margins =c(12,9),
          #Colv="NA", # only draw a row dendrogram
          hclust=function(x) hclust(x,method="complete"),
          distfun=function(x) as.dist((1-cor(t(x)))/2)
          ) # using pearson correlation as distance

Group <-c("Blood: Vector", "Blood: Vector", "Blood: E2", "Blood: E2",    
"Blood: E2+BP3",  "Blood: E2+BP3",  "Blood: Vector",  "Blood: Vector",
"Blood: E2", "Blood: E2", "Blood: E2+BP3",  "Blood: E2+BP3")

 groups = levels(as.factor(Group))
legend(0.2,0.1, groups, col=cols, pch=15, ncol = 3, cex=1)


This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: heatmaps_test2.png
Type: image/png
Size: 37145 bytes
Desc: heatmaps_test2.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20181004/14cbf315/attachment-0002.png>

More information about the R-help mailing list