Search code examples
rlegendcomplexheatmap

How to create discrete legend using Complexheatmap package (in R)


I have a matrix of count data that I'd like to show using a discrete legend, even though my transformed data (log(data+1)) is on a continuous scale. I tried using this line of code (lgd = Legend(at = 1:6, legend_gp = gpar(fill = 1:6))), but R returns an "unused" argument.

head(data)
  2019_dry 2019_wet 2020_dry 2020_wet
1        5        0       64        0        
2        3        9        4        4       
3       38       20       49        4       
4       14       17        2        2       
5        0        0        0        0        
6        2       10        0        0      



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

plot1<-Heatmap(as.matrix(log(data+1)),
            cluster_rows = FALSE,
            cluster_columns = FALSE,
            col = rev(rg),
        #-> lgd = Legend(at = 1:6, legend_gp = gpar(fill = 1:6)),
            rect_gp = gpar(col = "grey"),
            heatmap_legend_param = list(title = "ln(x+1)"))

Solution

  • I found this line of code worked:

    heatmap_legend_param = list(title = "ln(x+1)", color_bar = "discrete"),...