ComplexHeatmap - alter legend position
2
0
Entering edit mode
Lucy ▴ 60
@lucy-17014
Last seen 3 months ago
United Kingdom

Is there an easy way to move the legend to the right using ComplexHeatmap, so that it doesn't overlap with the row names? I have read the documentation, but am struggling to find the appropriate argument. The main heatmap code is below, along with a plot example.

annotation_col <- HeatmapAnnotation(
    Timepoint = metadata$timepoint, 
    col = list(Timepoint = time_cols), 
    show_legend = TRUE, 
    show_annotation_name = FALSE, 
    simple_anno_size = unit(4, "mm"), 
    annotation_name_gp = gpar(fontsize = 11))

my_heatmap <- Heatmap(
    my_matrix, 
    col = colorRampPalette(rev(
        RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(100), 
    show_column_names = FALSE, 
    top_annotation = annotation_col, 
    row_names_gp = gpar(fontsize = 10, fontface = 1), 
    column_title_gp = gpar(fontsize = 14), 
    heatmap_legend_param = list(
        title = "Activity", 
        title_gp = gpar(fontsize = 11), 
        labels_gp = gpar(fontsize = 11)),  
    split = 3, 
    row_title = NULL)

ComplexHeatmap example heatmap

ComplexHeatmap • 1.0k views
ADD COMMENT
1
Entering edit mode
Zuguang Gu ▴ 290
@zuguang-gu-7797
Last seen 25 days ago
Germany / Heidelberg / DKFZ

There is a default "maximal length of row names". You can set the argument row_names_max_width to a larger value:

Heatmap(..., row_names_max_width = unit(10, "cm"))

If you want to set such max width programmatically, you can do

max_width = grobWidth(textGrob(rownames(my_matrix), gpar(fontsize = 10, fontface = 1)))

You can also explicitly convert it to mm or inche since the current max_width object might be large.

max_width = convertWidth(max_width, "mm")

then

Heatmap(..., row_names_max_width = max_width)
ADD COMMENT
0
Entering edit mode

Is it possible to use the 'width' argument and then shift the whole heatmap to the left? Or is the only option to truncate the row names?

ADD REPLY
0
Entering edit mode

Great, thank you. The row_names_max_width argument seems to have worked!

ADD REPLY
0
Entering edit mode
@james-w-macdonald-5106
Last seen 2 days ago
United States

There is an argument to specify which side to put the legend on.

0
Entering edit mode

Thank you. It's not the side that I want to change. I want to move the legend further to the right, so that it doesn't overlap with the row names.

ADD REPLY

Login before adding your answer.

Traffic: 886 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