Heatmap legend position change
1
0
Entering edit mode
16 months ago
hellokwmin • 0

Hello,

This is code that I am using to generate a heatmap:

color_fun <- colorRamp2(c(-4, 0, 4), c("red", "black", "green"))
df = read.csv("RAT7basedlog2version2.csv", header = T)
df$Cluster = factor(df$Cluster, levels =c(
  "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
  "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
  "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
  "41", "42"
))


heat = as.matrix(df[, c("RAT7", "PTR7")])
rownames(heat) <- paste0(df$geneid)
ColAnn = data.frame(colnames(heat))
colnames(ColAnn) = c("Sample")
ColAnn = HeatmapAnnotation(df = ColAnn, which = "col")
RowAnn = data.frame(df$Cluster)
colnames(RowAnn) = ("Cluster")

colours = list("Cluster" = color_codes <- c(
  "1" = "#FF0000",   "2" = "#00FF00",   "3" = "#0000FF",   "4" = "#FFA500",
  "5" = "#FF00FF",   "6" = "#00FFFF",   "7" = "#800080",   "8" = "#008000",
  "9" = "#FF4500",   "10" = "#32CD32",  "11" = "#9400D3",  "12" = "#FF1493",
  "13" = "#8A2BE2",  "14" = "#FF69B4",  "15" = "#FF6347",  "16" = "#1E90FF",
  "17" = "#FFD700",  "18" = "#00CED1",  "19" = "#DC143C",  "20" = "#ADFF2F",
  "21" = "#FF8C00",  "22" = "#8B008B",  "23" = "#7FFF00",  "24" = "#4B0082",
  "25" = "#00FF7F",  "26" = "#FF4500",  "27" = "#800000",  "28" = "#808000",
  "29" = "#000080",  "30" = "#008080",  "31" = "#FFDAB9",  "32" = "#CD853F",
  "33" = "#FFC0CB",  "34" = "#BC8F8F",  "35" = "#FF7F50",  "36" = "#F08080",
  "37" = "#20B2AA",  "38" = "#7FFFD4",  "39" = "#DDA0DD",  "40" = "#87CEFA",
  "41" = "#FF6347",  "42" = "#1E90FF"
))

RowAnn = HeatmapAnnotation(df = RowAnn, col = colours, which = "row", show_annotation_name = F, simple_anno_size = unit(0.5, "cm"), annotation_height = unit(1, "cm"))


split = c(rep("A:1-992", 992), rep("B:993-1231", 239))

lgd = Legend(col_fun = color_fun, title = "Log2FC", direction = "horizontal")
lgd
hmap = Heatmap(heat,
               name = "Log2FC",
               col = color_fun,
               show_heatmap_legend = T,
               split = df$Cluster,
               cluster_columns = F,
               cluster_row_slices = F,
               cluster_rows = F,
               show_row_dend = F,
               show_row_names = F,
               show_column_names = F,
               show_column_dend = F,
               row_title_side = "left",
               row_title = NULL,
               row_title_gp = gpar(fontsize = 12, fontface = "bold"),
               row_names_side = "left",
               row_title_rot = 0,
               width = unit(4, "cm"),
               row_split = split,
               right_annotation = rowAnnotation(foo = anno_block(labels = c("", ""), width = unit(2, "cm"))))
draw(hmap+RowAnn, heatmap_legend_side = "left", annotation_legend_side = "right")

enter image description here

And, How can I move the legend that are makred as red box to above Cluster legend?; probably, I need to reduce the size of "Cluster" legend

ComplexHeatmap • 4.3k views
ADD COMMENT
1
Entering edit mode

I need to reduce the size of "Cluster" legend

Your legend should not have 42 possible values, that is not a legend. Unless you're explaining each cluster in detail somewhere (which I highly doubt), add a proper annotation to just the cluster(s) of interest and remove the cluster row annotation altogether. Even if you wanted to retain all cluster numbers, add them to the row annotation so the annotation is not just a colored block but a colored block with some text in it.

ADD REPLY
0
Entering edit mode

This looks like you're using the ComplexHeatmap package so I'd recommend reading through the excellently documented reference material, and if you're not using ComplexHeatmap, I'd recommend switching over to it. For example, there's an entire section on the position of legends.

https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html

ADD REPLY
0
Entering edit mode

RTFM is a good response but does not warrant being an answer, so I've moved it to a comment. OP definitely needs to read the freaking manual though.

ADD REPLY
1
Entering edit mode
16 months ago
bk11 ★ 3.0k

You can change show_heatmap_legend = F in your hmap = Heatmap(.......) section and plot legend separately.

draw(hmap+RowAnn, heatmap_legend_side = "left", annotation_legend_side = "right")
draw(lgd, x = unit(1, "cm"), y = unit(17.2, "cm")) ##you need to play with x = unit(1, "cm"), y = unit(17.2, "cm") section to position your legend where you wanted

Here is the demo how I ll do-

color_fun <- colorRamp2(c(-4, 0, 4), c("red", "black", "green"))
lgd = Legend(col_fun = color_fun, title = "Log2FC")

hmap <- Heatmap(matrix(rnorm(100), 10), show_heatmap_legend = F,col = color_fun,
    cluster_rows = F, cluster_columns = F,width = ncol(mat)*unit(20, "mm"), 
    height = nrow(mat)*unit(13, "mm"))
draw(hmap)
draw(lgd, x = unit(1, "cm"), y = unit(17.2, "cm"))

enter image description here

ADD COMMENT

Login before adding your answer.

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