rcomplexheatmap

Not getting correct row order in the row split ComplexHeatmap R


I am trying to get heatmap in a specific order (i.e. row and column order). Although I am giving all the necessary arguments (row order and row split), but the code is not generating the heatmap in the desired order. The code I am using is shown below:

dumm_data <- as.data.frame(read_xlsx("dummy_data.xlsx"))
dumm_data <- dumm_data[order(dumm_data$group3Vs0,decreasing=TRUE),]
rownames(dumm_data) <- dumm_data$feature
heatmap_roworder <- rownames(dumm_data)
dumm_data$feature <- NULL
col_fun = colorRamp2(c(min(dumm_data), 0, max(dumm_data)), transparency = 0.1, c("blue", "black", "red"))

heatmap_object <- Heatmap(
  mat = as.matrix(dumm_data),
  name = "FC",
  row_order = heatmap_roworder,
  show_row_names = FALSE,
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  show_row_dend = FALSE,
  show_column_dend = FALSE,
  row_split = ifelse(dumm_data$group3Vs0>0, "UP", "DOWN"),
  col = col_fun,
  column_order = colnames(dumm_data),
  column_labels = c("Gp0", "Gp1 vs 0", "Gp2 vs 0", "Gp3 vs 0"),
  column_split = c("Gp0", "Gp1 vs 0", "Gp2 vs 0", "Gp3 vs 0")
  )

# Draw the heatmap
draw(heatmap_object, heatmap_legend_side = "left")

Using the above code, I am getting the following heatmap (which is not what is expected)

dummy_data_heatmap

Expectations: 1. The row order should be UP and DOWN (not DOWN and UP).2. The ticks in color bar should be -8 to 8 (not -10 to 10). So, how can I change ticks to color bar to (-8,-6,-4,-2,-,2,4,6,8).

I very much appreciate the suggestions to improve the code. Thanks.

Data to reproduce is shown below (generated using dput(dumm_data):

dumm_data <- structure(list(group0 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), group1Vs0 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, -2.79968064074482, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3.77487088813133, 0, 0), group2Vs0 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.82060876346902, 
0, 0, 0, 0, 0, 0, 0, 0, 0.982271260086248, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.412563989748342, 
0, 0, 0, 0, 0, 0, 0, 0, -1.87587139798223, -2.10678273027217, 
-1.90461829339215, -2.32274679178662, -1.93984587454369, -1.9302179452785, 
1.66089155193753, -5.45981215420888, -1.92581168852915, -1.84823790984302, 
-2.40948386860016, -1.22672917914705, -0.733509783977281, 0, 
-1.33712158057336, -0.922475220604306, -1.91521503205084, -2.39847316389119, 
2.49681500900014, -1.52773939930416, -2.09447796292692, -2.08699927904513, 
-1.29145099887324, -1.61854231447379, -7.41515182278667, -3.56490928235142, 
2.02219631520954, -1.8082150483038, -3.66554649401191, -2.2715867982231, 
1.31147933987725, -2.23107405021695, 0.443688819475455, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.19209753798283, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, -1.70573453313667, 0, 0, 0, 0, 0, 0, 
0, -2.88600779588494, 0, 0, 0, 0, 0, 0, 0, 0), group3Vs0 = c(7.92995195977545, 
5.2271603483093, 5.05080090237118, 4.33569378772174, 4.13269767397466, 
4.08426824019154, 4.01237968454507, 3.90672332861797, 3.89267435289161, 
3.80443523958511, 3.77701214522716, 3.679658536757, 3.67432403653779, 
3.63690901076509, 3.51076095563822, 3.35208493657741, 2.39461402358594, 
2.37349513306282, 2.30519480397738, 2.24681463336124, 2.24449449185484, 
2.22720710438224, 2.21912208381213, 2.18857990995778, 2.08241508742372, 
2.07291416214736, 2.01298043941906, 1.72156985048669, 1.26903689196904, 
1.24446662520034, 1.17773703508968, 1.10276443386246, 1.0895001885069, 
1.086033532513, 1.08505503306484, 1.06169271620403, 1.04018951727824, 
1.03847494334488, 0.972350531051984, 0.959299900565939, 0.948443672858841, 
0.89937026936513, 0.890937084597762, 0.871628571921023, 0.859181519622412, 
0.808253534172529, 0.794958219507581, 0.774687725354181, 0.74788405365276, 
0.69486036602269, 0.670293243182336, 0.668638417339404, 0.617999180611024, 
0.614886026166409, 0.606219229498668, 0.568986568551615, 0.559013209341914, 
0.554144528802557, 0.518697975219195, 0.317312293678904, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.509192043139633, -0.526846059705124, 
-1.35604140544007, -1.4564786873651, -1.45868708960597, -1.45946598241657, 
-1.482465574264, -1.48437050030245, -1.50208515757889, -1.52987036240135, 
-1.5606530152192, -1.58241444970407, -1.62828311715674, -1.7506612085203, 
-2.82622367283819, -2.88715589915432, -2.91152020246713, -2.91617980543651, 
-2.92953892975804, -2.99865378129609, -3.04261692119407, -3.04348640783662, 
-3.08754134981857, -3.12354933116662, -3.19616382200461, -3.35659192425397, 
-3.36036458614177, -3.37688164385444, -3.62536897490944, -3.62910512792756, 
-4.08071241424145, -4.2350408721331, -4.33272147389831, -4.35816724902832, 
-4.60150193339107, -4.65616093389606, -5.49488809420453, -5.60218026106905, 
-5.65447303056075, -5.76604859676916, -6.44508056924528, -7.58382604652574
)), row.names = c("f135", "f134", "f133", "f132", "f131", "f130", 
"f129", "f128", "f127", "f126", "f125", "f124", "f123", "f122", 
"f121", "f120", "f119", "f118", "f117", "f116", "f115", "f114", 
"f113", "f112", "f111", "f110", "f109", "f108", "f107", "f106", 
"f105", "f104", "f103", "f102", "f101", "f100", "f99", "f98", 
"f97", "f96", "f95", "f94", "f93", "f92", "f91", "f90", "f89", 
"f88", "f87", "f86", "f85", "f84", "f83", "f82", "f81", "f80", 
"f79", "f78", "f77", "f76", "f43", "f44", "f45", "f46", "f47", 
"f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55", "f56", 
"f57", "f58", "f59", "f60", "f61", "f62", "f63", "f64", "f65", 
"f66", "f67", "f68", "f69", "f70", "f71", "f72", "f73", "f74", 
"f75", "f42", "f41", "f40", "f39", "f38", "f37", "f36", "f35", 
"f34", "f33", "f32", "f31", "f30", "f29", "f28", "f27", "f26", 
"f25", "f24", "f23", "f22", "f21", "f20", "f19", "f18", "f17", 
"f16", "f15", "f14", "f13", "f12", "f11", "f10", "f9", "f8", 
"f7", "f6", "f5", "f4", "f3", "f2", "f1"), class = "data.frame")

Solution

  • I think the following changes solve your issues. Note that I put the changes as the two very last arguments in the Heatmap-call.

    1. I changed the character row_split to a factor (i.e., an ordered character)
    2. I included a heatmap_legend_param argument as instructed by the manual which offers to pass a list containing parameters for the heatmap legends to the color_mapping_legend method. Studying the possible arguments of color_mapping_legend I came up with the at argument.

    dumm_data <- structure(list(group0 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                                group1Vs0 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2.79968064074482, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3.77487088813133, 0, 0), 
                                group2Vs0 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.82060876346902, 0, 0, 0, 0, 0, 0, 0, 0, 0.982271260086248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.412563989748342, 0, 0, 0, 0, 0, 0, 0, 0, -1.87587139798223, -2.10678273027217, -1.90461829339215, -2.32274679178662, -1.93984587454369, -1.9302179452785, 1.66089155193753, -5.45981215420888, -1.92581168852915, -1.84823790984302, -2.40948386860016, -1.22672917914705, -0.733509783977281, 0, -1.33712158057336, -0.922475220604306, -1.91521503205084, -2.39847316389119, 2.49681500900014, -1.52773939930416, -2.09447796292692, -2.08699927904513, -1.29145099887324, -1.61854231447379, -7.41515182278667, -3.56490928235142, 2.02219631520954, -1.8082150483038, -3.66554649401191, -2.2715867982231, 1.31147933987725, -2.23107405021695, 0.443688819475455, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.19209753798283, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1.70573453313667, 0, 0, 0, 0, 0, 0, 0, -2.88600779588494, 0, 0, 0, 0, 0, 0, 0, 0), 
                                group3Vs0 = c(7.92995195977545, 5.2271603483093, 5.05080090237118, 4.33569378772174, 4.13269767397466, 4.08426824019154, 4.01237968454507, 3.90672332861797, 3.89267435289161, 3.80443523958511, 3.77701214522716, 3.679658536757, 3.67432403653779, 3.63690901076509, 3.51076095563822, 3.35208493657741, 2.39461402358594, 2.37349513306282, 2.30519480397738, 2.24681463336124, 2.24449449185484, 2.22720710438224, 2.21912208381213, 2.18857990995778, 2.08241508742372, 2.07291416214736, 2.01298043941906, 1.72156985048669, 1.26903689196904, 1.24446662520034, 1.17773703508968, 1.10276443386246, 1.0895001885069, 1.086033532513, 1.08505503306484, 1.06169271620403, 1.04018951727824, 1.03847494334488, 0.972350531051984, 0.959299900565939, 0.948443672858841, 0.89937026936513, 0.890937084597762, 0.871628571921023, 0.859181519622412, 0.808253534172529, 0.794958219507581, 0.774687725354181, 0.74788405365276, 0.69486036602269, 0.670293243182336, 0.668638417339404, 0.617999180611024, 0.614886026166409, 0.606219229498668, 0.568986568551615, 0.559013209341914, 0.554144528802557, 0.518697975219195, 0.317312293678904, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.509192043139633, -0.526846059705124, -1.35604140544007, -1.4564786873651, -1.45868708960597, -1.45946598241657, -1.482465574264, -1.48437050030245, -1.50208515757889, -1.52987036240135, -1.5606530152192, -1.58241444970407, -1.62828311715674, -1.7506612085203, -2.82622367283819, -2.88715589915432, -2.91152020246713, -2.91617980543651, -2.92953892975804, -2.99865378129609, -3.04261692119407, -3.04348640783662, -3.08754134981857, -3.12354933116662, -3.19616382200461, -3.35659192425397, -3.36036458614177, -3.37688164385444, -3.62536897490944, -3.62910512792756, -4.08071241424145, -4.2350408721331, -4.33272147389831, -4.35816724902832, -4.60150193339107, -4.65616093389606, -5.49488809420453, -5.60218026106905, -5.65447303056075, -5.76604859676916, -6.44508056924528, -7.58382604652574 )), 
                           row.names = c("f135", "f134", "f133", "f132", "f131", "f130", "f129", "f128", "f127", "f126", "f125", "f124", "f123", "f122", "f121", "f120", "f119", "f118", "f117", "f116", "f115", "f114", "f113", "f112", "f111", "f110", "f109", "f108", "f107", "f106", "f105", "f104", "f103", "f102", "f101", "f100", "f99", "f98", "f97", "f96", "f95", "f94", "f93", "f92", "f91", "f90", "f89", "f88", "f87", "f86", "f85", "f84", "f83", "f82", "f81", "f80", "f79", "f78", "f77", "f76", "f43", "f44", "f45", "f46", "f47", "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55", "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63", "f64", "f65", "f66", "f67", "f68", "f69", "f70", "f71", "f72", "f73", "f74", "f75", "f42", "f41", "f40", "f39", "f38", "f37", "f36", "f35", "f34", "f33", "f32", "f31", "f30", "f29", "f28", "f27", "f26", "f25", "f24", "f23", "f22", "f21", "f20", "f19", "f18", "f17", "f16", "f15", "f14", "f13", "f12", "f11", "f10", "f9", "f8", "f7", "f6", "f5", "f4", "f3", "f2", "f1"), 
                           class = "data.frame")
    
    dumm_data <- dumm_data[order(dumm_data$group3Vs0, decreasing=TRUE),]
    rownames(dumm_data) <- dumm_data$feature
    heatmap_roworder <- rownames(dumm_data)
    dumm_data$feature <- NULL
    col_fun <- colorRamp2::colorRamp2(c(-8, 0, 8), transparency = 0.1, c("blue", "black", "red"))
    
    heatmap_object <- ComplexHeatmap::Heatmap(
      mat = as.matrix(dumm_data),
      name = "FC",
      row_order = as.numeric(heatmap_roworder),
      show_row_names = FALSE,
      cluster_rows = FALSE,
      cluster_columns = FALSE,
      show_row_dend = FALSE,
      show_column_dend = FALSE,
      col = col_fun,
      column_order = colnames(dumm_data),
      column_labels = c("Gp0", "Gp1 vs 0", "Gp2 vs 0", "Gp3 vs 0"),
      column_split = c("Gp0", "Gp1 vs 0", "Gp2 vs 0", "Gp3 vs 0"),
      
      row_split = factor(ifelse(dumm_data$group3Vs0 > 0, "UP", "DOWN"), levels = c("UP", "DOWN")), 
      heatmap_legend_param = list(at = c(-8, -4, 0, 4, 8))
    )
    
    # Draw the heatmap
    draw(heatmap_object, heatmap_legend_side = "left")
    

    enter image description here