0

I want to trying to wtire a function that will generate heatmaps by calling heatmap.2. Among other things, I want to generate the title of the plots by calling add.expr, for example:

add.expr=c(mtext(text=titlestring, side=3, line=4, cex=1)

With titlestring being a charqacter vector passed to the function by another function:

titlestring<-paste("Mean bin methylation",samplename, "on 5kb flanked CpG Island promoters in mm9")

When I try to run my function I get the following error:

Error in as.graphicsAnnot(text) : object 'titlestring' not found I do know that titlestring is defined in the scope of my funciton , as I tested this using print() I thouhgt that the problem my be related to the fact the mtext() expects an exprssion object, so I coerced titlestring to an expression using as.expression(). But I still get this error.

Any idea what might be the problem?

Thanks in advance

Dolev Rahat

user1614062
  • 182
  • 2
  • 13
  • 1
    Your titlestring command gives an error because samplename is not known. Why not use something like main=titlestring in the heatmap call? – Ruthger Righart Feb 25 '15 at 12:29
  • @RuthgerRighart: Thanks. Using main is indeed a possibility, but since I want to add a secondary title I still prefer to use add.expr() because it is more fleixble. samplename is a character vector geneated by my calling function based on one of its arguments, so it is defined (as I stated, I also verified that titlestring is visible to the internal function). Is there no possibilty to call mtext() with character vectors passed from another function? – user1614062 Feb 25 '15 at 14:14
  • 1
    I am not sure about what you want exactly, it would be good to have a data example. Please find a possible workaround in the answer box, hope that helps ! – Ruthger Righart Feb 25 '15 at 14:42
  • @RuthgerRighart: I added an example of what I want below. However, I think the difference between what I want to do using add.expr() and what I get using main is not that crucial, so I will not waste any furhter time on this. Thank you very much for all your help! – user1614062 Feb 25 '15 at 15:58

2 Answers2

0

If your main question is about adding a secondary title using a function you could use main = c(titlestring, substring), where you can change the vector strings as you like. The example is taken from the mtcars set (it can be found using ?heatmap).

require(graphics) 
require(grDevices)

x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)

titlestring<-c("Mtcars dataset")
substring<-c("First example")
par(cex.main=0.9)
heatmap(x, Colv = NA, col = cm.colors(256), scale = "column",
    RowSideColors = rc, margins = c(5,10),
    xlab = "specification variables", ylab =  "Car Models",
    main = c(titlestring, substring))

enter image description here)

Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33
0

Not that worth all this fuss, but Ideally i would like to be able to have different font sizes for the main and secndary title, and also to control thier position. I could do this if I added the title with add.expr() using litterals, for example:

 heatmap.2 (as.matrix(matToPlot[,(1:totCols)]),
         Rowv=FALSE, Colv=FALSE, dendrogram="none", 
         breaks=seq(0,1,by=1/length(methColors)),
         col=methColors, trace="none",colsep=colsep,sepcolor=c("sky blue"),
         #main=paste(titlestring,subtitlestring,chromstring,sep="\n"),
         add.expr=c(mtext(text="Read coverage of Dox Plus on 5kb flanked CpG
          Island promoters in mm9", side=3, line=4, cex=1),
         mtext(text="Island set represents island for which ChIP data is avilable
          and is order by mean(H3K4me3)-mean(H3K27me3) within islands", side=3, line=2, cex=0.8),
         mtext(text="H3K27me3", side=1, adj=0.125, line=3),
         mtext(text="H3K4me3", side=1, adj=0.375, line=3),
         mtext(text="H3K4me1", side=1, adj=0.625, line=3),
         mtext(text="Methylation", side=1,adj=0.9, line=3))) 

Whereas using main I get a result which I can live with, but which is not as good (compare these two plots):

plot generated using add.expr

plot generated using main

user1614062
  • 182
  • 2
  • 13