将使用 paste() 生成的字符传递给 mtext()

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

我想尝试调用 heatmap.2 生成热图的函数。除其他事项外,我想通过调用 add.expr 生成绘图的标题,例如:

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

titlestring 是一个由另一个函数传递给该函数的字符向量:

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

当我尝试 运行 我的函数时,出现以下错误:

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.

知道可能是什么问题吗?

提前致谢

多列夫·拉哈特

如果您的主要问题是关于使用函数添加次要标题,您可以使用 main = c(titlestring, substring),您可以在其中随意更改矢量字符串。该示例取自 mtcars 集(可以使用 ?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))

)

不值得大惊小怪,但理想情况下,我希望能够为主要和次要标题设置不同的字体大小,并控制它们的位置。如果我使用文字添加带有 add.expr() 的标题,我可以这样做,例如:

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

虽然使用 main 我得到了一个我可以接受的结果,但它不是那么好(比较这两个图):