Pergunta

I'm creating an heatmap through heatmap.2(). I'd like to put the xaxis labels rotate of 45 degree. Following the instructions in other posts, I tried to do this building an heatmap without x labels and then using text() to add them... This is what I tried:

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

This put the labels on the heatmap, but all the names were overlying... I also tried this:

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

The result was better, since the labels were along the axis, but still really close among them and overlying the plot...

Is there anything wrong in the way I used locator() to find the coordinates? Can anyone help me to improve my code?

Foi útil?

Solução

You can play with argument pos in your call to text. By using pos=1 for example:

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

enter image description here

See ?text for more on pos.

If the labels fall outside the plot, you can try using xpd=NA to clip them to the device region instead of the plot or the figure regions.

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))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top