Search code examples
rexpressionheatmapgplots

passing a character generated using paste() to mtext()


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


Solution

  • 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)