避免对热图中的行和列进行重新排序(r)

时间:2019-04-22 18:49:09

标签: r matrix heatmap bioconductor

我正在使用ComplexHeatmap绘制数据,但是,每当我开始划分矩阵以便更容易阅读时,我的行/列的顺序就会被打乱。

我想知道是否可能我没有正确指定要使用的顺序,而不是直接与ComplexHeatmap使用有关的问题。例如,以下内容是否有问题:

    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")),
    )

以下参数是该问题的关键:

                 # ! ---> 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))),

创建此热图时,行和列的顺序不正确。例如

enter image description here

现在,当我删除row_splitcolumn_split命令时,不需要设置row_ordercolumns_order的顺序,它看起来像这样:

enter image description here

此后,我发现当我不分割使用row_ordercolumn_order时,实际上仍然会打乱我的订单吗?

enter image description here

很显然,我什至无法按需保持命令的原始顺序。我想知道是否有人可以对此提供见识。

这是一个示例矩阵,可以根据他们的supporting documents来制作(它们确实涵盖了row_order,但我必须忽略了某些内容)。您可能希望避免改组名称,但是鉴于我自己甚至不能使用命令,因此我担心修改它。

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)

以下是我一直在尝试的一些示例:

                 #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))),

1 个答案:

答案 0 :(得分:0)

我认为这是如何正确设置分割热图的变量。

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
    )

enter image description here