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

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Thu Oct 4 21:08:53 CEST 2018


It will take a bit of trial and error. Use this example as a guide. You will probably have to set the y coordinate to a negative value and include xpd=TRUE:

> data(mtcars)
> x  <- as.matrix(mtcars)
> heatmap.2(x)
> legend(.5, -.1, "Legend", xpd=TRUE)

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Ding, Yuan Chun
Sent: Thursday, October 4, 2018 1:19 PM
To: r-help using r-project.org
Subject: Re: [R] heatmap.2, adding a legend at bottom


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,

Ding


----------------------------------------------------------------------------
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
          breaks=col_breaks,
          cexRow=1.4,
          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)
dev.off()



------------------------------------------------------------
-SECURITY/CONFIDENTIALITY WARNING-  

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)




More information about the R-help mailing list