rlegendcomplexheatmap

Set limits of ComplexHeatmap legend in R


I'm working in R using the library(ComplexHeatmap). How would I manually set the legend to go from 0 -> 100 (%)? My actual data only ranges from about 0-40%.

df <- matrix((rnorm(100)^2)*9, nrow = 10)

rg<-brewer.pal(n = 8, name = "RdYlGn")

Heatmap(as.matrix(round(df, digits = 1)),
        cluster_rows = FALSE,
        cluster_columns = FALSE,
        col = rev(rg),
        rect_gp = gpar(col = "black"),
        row_title_gp = gpar(fontsize = 7),
        heatmap_legend_param = list(title = "% cover"))

Solution

  • You can use the at argument inside the list passed to th heatmap_legend_param argument

    library(ComplexHeatmap)
    library(RColorBrewer)
    
    set.seed(1)
    
    df <- matrix((rnorm(100)^2) * 6, nrow = 10)
    
    rg<-brewer.pal(n = 8, name = "RdYlGn")
    
    Heatmap(as.matrix(round(df, digits = 1)),
            cluster_rows = FALSE,
            cluster_columns = FALSE,
            col = rev(rg),
            rect_gp = gpar(col = "black"),
            row_title_gp = gpar(fontsize = 7),
            heatmap_legend_param = list(title = "% cover", at = seq(0, 100, 10)))
    

    Created on 2022-07-21 by the reprex package (v2.0.1)