简体   繁体   中英

Avoid Re-Ordering of Rows and Columns in a Heatmap (r)

I am using ComplexHeatmap to plot data, however, whenever I begin to divide matrix so it can be more easily read, the order of my rows/columns appear to be shuffled.

I wonder if perhaps I am not correctly specifying what order to use, rather than this being an issue related directly to ComplexHeatmap usage. For instance, whether there was something wrong with the following:

    Heatmap( as.matrix(MyData), name = "q", 
             column_names_rot = 0,
             col = my_palette,
             # ! ---> This should set the row order!!
             row_order = order(rownames(as.matrix(MyData))),
             # ! ---> This should set the column order!!
             column_order = order(colnames(as.matrix(MyData))),
             row_split = rep(c("A", "B","C", "D"), 6),
             column_split = rep(c("A", "B","C", "D"), 6),
             cluster_row_slices = FALSE,
             cluster_column_slices =FALSE,
             heatmap_legend_param = list(
               title = "Legend",
               col = my_palette,
               at = (c(0.05,0.03,0.01,0.000001)), 
               legend_height = unit(6, "cm")),
    )

With the following arguments being key to this question:

                 # ! ---> This should set the row order!!
                 row_order = order(rownames(as.matrix(MyData))),
                 # ! ---> This should set the column order!!
                 column_order = order(colnames(as.matrix(MyData))),

When I create this Heatmap the rows and columns are not in order. eg

在此处输入图片说明

Now when I remove the row_split and column_split command, I have no need to set row_order and columns_order order and it looks like this:

在此处输入图片说明

I have since found that when I use row_order and column_order without splitting, it still, in fact, shuffles my order?

在此处输入图片说明

Clearly, I'm not even able to keep the original order by command when I want. I wondered if someone might provide insight into this.

Here is an example matrix that can be made from their supporting documents (they do cover row_order but I must be overlooking something). You may want to avoid the reshuffling of names but I'm nervous about modifying it given that I can't even use the commands myself.

library(devtools)
install_github("jokergoo/ComplexHeatmap", force = TRUE)
library(ComplexHeatmap)

set.seed(123)
nr1 = 4; nr2 = 8; nr3 = 6; nr = nr1 + nr2 + nr3
nc1 = 6; nc2 = 8; nc3 = 10; nc = nc1 + nc2 + nc3
mat = cbind(rbind(matrix(rnorm(nr1*nc1, mean = 1,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc1, mean = 0,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc1, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc2, mean = 0,   sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc2, mean = 1,   sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc2, mean = 0,   sd = 0.5), nr = nr3)),
    rbind(matrix(rnorm(nr1*nc3, mean = 0.5, sd = 0.5), nr = nr1),
          matrix(rnorm(nr2*nc3, mean = 0.5, sd = 0.5), nr = nr2),
          matrix(rnorm(nr3*nc3, mean = 1,   sd = 0.5), nr = nr3))
   )
mat = mat[sample(nr, nr), sample(nc, nc)] # random shuffle rows and columns
rownames(mat) = paste0("row", seq_len(nr))
colnames(mat) = paste0("column", seq_len(nc))

Heatmap(mat)

Here are a few examples of what I have been trying:

                 #row_order = sort(rownames(as.matrix(MyData))),
                 #row_order = rownames((MyData)),
                 #row_order = sort(rownames(MyData)),
                 #row_order = order((gsub("row", "", rownames(MyData)))),
                 #row_order = order(colnames(as.matrix(MyData))),
                 #column_order = order(rownames(as.matrix(MyData))),
                 #column_order = colnames(MyData),
                 #row_order = order(rownames(MyData)),
                 #column_order = order(as.matrix(MyData)),
                 #row_order = order(rownames(MyData)),
                 #column_order = order(colnames(MyData)),
                 #row_order = sort(c("C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6")),
                 #olumn_order = sort(c("C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6", "C1", "C2", "C3", "C4", "C5", "C6")),
                 #column_order = order(colnames(MyData)),
                 #column_order = sort(as.numeric(as.matrix(MyData))),
                 #column_order = order(colnames(MyData)),
                 #row_order = (MyData),
                 #column_order = sort(colnames(as.matrix(MyData))),
                 #row_order = rownames(MyData),
                 #row_order = sort(gsub("row", "", rownames(MyData))),
                 #row_order = order(colnames(as.matrix(MyData))),
                 #column_order = sort(colnames(as.matrix(MyData))),
                 #row_order = order(as.numeric(gsub("row", "", rownames(as.matrix(MyData))))), 
                 #column_order = order(as.numeric(gsub("column", "", colnames(as.matrix(MyData))))),
                 #row_order = order(rownames(as.matrix(MyData))), 
                 #column_order = order(colnames(as.matrix(MyData))),
                 #row_order = order(rownames(as.matrix(MyData))), 
                 #column_order = order(colnames(as.matrix(MyData))),

I think the thing is how you properly set the variable that splits the heatmap.

Heatmap( (MyData), name = "namehere", 
             column_names_rot = 0,
             col = my_palette,
             cluster_rows = FALSE,
             cluster_columns = FALSE,
             row_split = rep(1:4, each = 6),
             column_split = rep(1:4, each = 6),
             heatmap_legend_param = list(
               title = "Legend",
               col = my_palette,
               at = (c(0.05,0.03,0.01,0.000001)), 
               legend_height = unit(6, "cm")),
            border = TRUE
    )

在此输入图像描述

A bit late to the game here but the problem you are encountering here I believe is due to the nascent ordering of the factor. Specifically, row_ or column_split should be a factor, and the levels of the factor should correspond to the partitions of rows or columns that you want in your Heatmap. The order of the levels is what dictates how they will be represented in the Heatmap, although within each level/split the ordering from the expression matrix is maintained. So the solution is simply to make sure your row_ or column_split is a factor and set the order appropriately. For example, if you have a column_split vector that looks like x<-c("C", "A", "B") and you def want that ordering respected, then something like column_split =factor(x, levels=unique(x)) should work since unique() respects the ordering of the input vector (in this case x).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM