rcomplexheatmap

Barplot ComplexHeatmap


I did a barplot as column annotation on a heatmap. I use ComplexHeatmap.

My input for annotation is:

vector_pvalues_adj <- c(0.3778364, 0.0001000, 0.2122000, 0.4174714, 0.3778364, 0.4799250, 0.1613250, 0.4861000, 0.4174714, 0.1008000, 0.0141000, 0.4174714, 0.0001000, 0.0018000, 0.4861000, 0.4799250, 0.0001000, 0.0001000)

And the code is:

library(ComplexHeatmap)
column_ha3 = HeatmapAnnotation("-log adj p-value"= anno_barplot(-log(vector_pvalues_adj)), gp = gpar(fill = "red"), height =  unit(20, "mm"))

enter image description here

I would to add an abline

abline(h = -log(0.05), col= "red")

enter image description here

But I'm not able, could someone suggest to me how to do it?


Solution

  • In order to add something like a horizontal line, you can use the ComplexHeatmap function decorate_annotation and the grid package.

    This adds a dashed line.

    library(ComplexHeatmap) # BiocManager::install("ComplexHeatmap")
    library(grid)
    
    # original code from question
    vector_pvalues_adj <- c(0.3778364, 0.0001000, 0.2122000, 0.4174714, 
                            0.3778364, 0.4799250, 0.1613250, 0.4861000,
                            0.4174714, 0.1008000, 0.0141000, 0.4174714,
                            0.0001000, 0.0018000, 0.4861000, 0.4799250,
                            0.0001000, 0.0001000)
    
    # original code from question
    column_ha3 = HeatmapAnnotation(
      "-log adj p-value" = anno_barplot(-log(vector_pvalues_adj)), 
      gp = gpar(fill = "red"), height =  unit(20, "mm"))
    
    # added for context
    Heatmap(matrix(rnorm(18*18), 18), name = "mat", top_annotation = column_ha3)
    
    # 'decorate' "-log adj p-value"
    decorate_annotation("-log adj p-value", {
      grid.lines(c(.5, 18.5), c(4, 4), gp = gpar(lty = 2, col = "red"),
                 default.units = "native")
    })
    

    In the last function, c(.5, 18.5) is the x-axis values, which is always equivalent to the number of columns, or 1:18 based on your data (using .5 get's you to the edges).

    For y, I have it set to start at 4 and end at 4, so you get a horizontal line. in gpar I used lty = 2 which gave me a dashed line instead of the default, a solid line.)

    enter image description here