【问题标题】:add text on heatmap using locator()使用 locator() 在热图上添加文本
【发布时间】:2013-10-03 19:26:50
【问题描述】:

我正在通过heatmap.2() 创建热图。我想把 xaxis 标签旋转 45 度。按照其他帖子中的说明,我尝试构建一个没有 x 标签的热图,然后使用text() 添加它们... 这是我尝试过的:

#fake matrix
cheese.matrix <- matrix(runif(100),10,10)
#build color palette
my.palette <- colorRampPalette(c("blue", "green", "yellow", "orange", "red"), space="rgb")
#build a first heatmap
hm_cheese <- heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
                       density.info=c("none"),margins(3,5),cexRow=0.8,
                       cexCol=0.8,key=TRUE,keysize=1,trace="none",
                       lhei=c(2,8), breaks=100)
#find the coordinates on the plot where I want to pu the first and the last label
pos2 <- locator()
pos2
$x
[1] 0.08129779 0.90164993

$y
[1] -0.06905376 -0.06372554

pos2 <- structure(list(x=c(0.08129779, 0.90164993), y=c(-0.06905376, -0.06372554)), .Names=c("x","y"))
#create a vector with the labels I want to add
labs <- c("NWC1.PR", "CURD1.PR", "NWC2.PR","CURD2.PR","NWC3.PR","CURD3.PR",  "NWC4.PR", "CURD4.PR", "NWC5.PR", "CURD5.PR")
#build another heatmap
hm_cheese <- heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
                      density.info=c("none"),margins(3,5),key=TRUE,
                      keysize=1,trace="none", lhei=c(2,8), breaks=100, 
                      labCol="", add.expr=text(x=seq(pos2$x[1], pos2$x[2], len=10),
                      y=rep(pos2$y[1],10), srt=45, xpd=TRUE, adj=0, labels=labs))

这将标签放在热图上,但所有名称都覆盖了...... 我也试过这个:

hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
                       density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
                       trace="none", lhei=c(2,8), breaks=100, labCol="", 
                       add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
                       xpd=TRUE, adj=0, labels=labs))

结果更好,因为标签是沿着轴的,但它们之间仍然非常接近并覆盖了情节......

我使用locator() 查找坐标的方式有什么问题吗? 谁能帮我改进我的代码?

【问题讨论】:

  • 我对 cheese.matrix 的想法很感兴趣。

标签: r text orientation heatmap labels


【解决方案1】:

您可以在调用text 时使用参数pos。例如使用pos=1

hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
                       density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
                       trace="none", lhei=c(2,8), breaks=100, labCol="", 
                       add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
                       xpd=TRUE, adj=0, labels=labs, pos=1))

有关pos 的更多信息,请参阅?text

如果标签落在绘图之外,您可以尝试使用xpd=NA 将它们剪辑到设备区域而不是绘图或图形区域。

hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
                       density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
                       trace="none", lhei=c(2,8), breaks=100, labCol="", 
                       add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
                       xpd=NA, adj=0, labels=labs, pos=1))

【讨论】:

  • 嗨,plannapus!感谢您的帮助!使用 pos 确实改善了这种情况,但是由于我的标签比示例中使用的 10 个标签多(我的真实矩阵有 50 列),所以第一个标签的最后一部分被剪掉了!所以它没有进入情节区域......我也尝试使用边距(0,0)或减小字体大小,但第一个实验室的一部分总是在外面......有没有办法让更大的地块面积?
  • @FrancescadeFilippis 查看编辑:最简单的方法是将标签剪辑到设备上,而不是使用xpd=NA 剪辑到图形上。但是,如果您确实有很多标签,我的建议是不要将它们绘制成 45°,而是垂直绘制以提高易读性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2016-01-04
相关资源
最近更新 更多