Position of the legend object in complexHeatmap
1
0
Entering edit mode
@3f9f9566
Last seen 8 weeks ago
Germany

I have made a very, very large heatmap. I am almost done, I love the way it looks. However, I am never managing to put the legend (separate object) where I want.

I made the legend like this :

lgd = Legend(title = "Log 2 fold-change", col_fun = colorRamp2(c(-3, 0, +3), c("#00aba9", "white", "#ff0097")),at = seq(-4,+4,by=2))

lgd2=Legend(title="Gene type",labels = c("protein-coding", "non-coding RNA", "pseudo gene"),legend_gp = gpar(fill =c("#673888","#c0c5ce","#eae374"))) 

pd=packLegend(lgd,lgd2,direction="horizontal")

Then the heatmap :


ha = HeatmapAnnotation(foo = anno_block(gp = gpar(fill = c("#f7a583","#00ff83")),labels = c("F6", "F0"), labels_gp = gpar(col = "black", fontsize = 25)),show_legend=FALSE) 

ha2 = HeatmapAnnotation(df = annot_row, which="row",show_annotation_name = FALSE,show_legend=FALSE, annotation_legend_param=list(title= "gene type"),col=col_types,width = unit(7, "cm"))


tryout<-Heatmap(cent_expr_matrix, name = "Log2FC", km = 5, col = colorRamp2(c(-3, 0, +3), c("#00aba9", "white", "#ff0097")),
    top_annotation = ha, column_split=annot_col$generation, right_annotation=ha2,show_row_names = TRUE, show_column_names=TRUE,show_column_dend=FALSE,show_row_dend=TRUE, 
    row_dend_width = unit(50, "mm"), column_title=NULL, width = unit(0.8, "snpc"), clustering_distance_rows = "pearson", show_heatmap_legend = FALSE)

# save the very long heatmap : 

cairo_pdf("/home/heatmap_try.pdf",width = 40, height = 345,onefile = FALSE) 
draw(tryout, heatmap_legend_list = pd,heatmap_legend_side = "left") 
dev.off()

Which puts the legend on the left, at half the height of the heatmap : enter image description here

I would like to be able to make it bigger, and at the top left side of the heatmap. But I am not finding how to do.

ComplexHeatmap • 833 views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 15 hours ago
United States

There are grid_height and grid_width arguments to Legend. Have you tried them?

0
Entering edit mode

Thank you I just did ! I think I am managing to make a bigger legend, but it is not changing its position though.

ADD REPLY
0
Entering edit mode

There is a 'just' argument for draw that has as its default 'center'. Getting to the corresponding help page is not intuitive though. If you do ?draw, the help page will tell you that there is an S4 method for Legends objects which indirectly points to the draw,Legends-method help. But getting to that help page can be a pain. For example, on Windows using Emacs/ESS, ?'draw,Legends-method' just hangs, and I have to use help("draw,Legends-method", package = "ComplexHeatmap") instead. Maybe it's different on RStudio, but I am an old dog and refuse to learn that new trick. Anyway,

> help("draw,Legends-method", package = "ComplexHeatmap")
draw,Legends-method       package:ComplexHeatmap       R Documentation

Draw the Legends

Description:

     Draw the Legends

Usage:

     ## S4 method for signature 'Legends'
     draw(object, x = unit(0.5, "npc"), y = unit(0.5, "npc"), just = "centre", test = FALSE)

Arguments:

  object: The 'grob' object returned by 'Legend' or 'packLegend'.

       x: The x position of the legends, measured in current viewport.

       y: The y position of the legends, measured in current viewport.

    just: Justification of the legends.

    test: Only used for testing.

Details:

     In the legend grob, there should always be a viewport attached
     which is like a wrapper of all the graphic elements in a legend.
     If in the 'object', there is already a viewport attached, it will
     modify the 'x', 'y' and 'valid.just' of the viewport. If there is
     not viewport attached, a viewport with specified 'x', 'y' and
     'valid.just' is created and attached.

     You can also directly use 'grid.draw' to draw the legend object,
     but you can only control the position of the legends by first
     creating a parent viewport and adjusting the position of the
     parent viewport.

Examples:

     lgd = Legend(at = 1:4, title = "foo")
     draw(lgd, x = unit(0, "npc"), y = unit(0, "npc"), just = c("left", "bottom"))

     # and a similar version of grid.draw
     pushViewport(viewport(x = unit(0, "npc"), y = unit(0, "npc"), just = c("left", "bottom")))
     grid.draw(lgd)
     popViewport()
ADD REPLY
0
Entering edit mode

I realize now how naive my question was. It is pretty much only a matter of adjusting x and y. The combinations of x and y in the draw call and in pushViewport where putting my legend far outside of the plot window. Thank you again !

ADD REPLY

Login before adding your answer.

Traffic: 645 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6