【问题标题】:How to label some rowname of heatmap in bold and other in italic如何以粗体和斜体标记热图的某些行名
【发布时间】:2021-09-14 13:35:40
【问题描述】:

我发现了一个有趣的函数,可以用粗体标记热图的一些行名。 我已经对其进行了调整,使其以斜体形式执行相同的操作,并且我想知道如何才能更多地调整此代码,以便将某些行名以粗体显示,而将其他行名以斜体显示。请找到这两个函数和热图(我正在使用找到的相同示例来解释该函数)。

library(pheatmap)
library(tidyverse)

# Create test matrix
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")


make_bold_names <- function(mat, rc_fun, rc_names) {
  bold_names <- rc_fun(mat)
  ids <- rc_names %>% match(rc_fun(mat))
  ids %>%
    walk(
      function(i)
        bold_names[i] <<-
        bquote(bold(.(rc_fun(mat)[i]))) %>%
        as.expression()
    )
  bold_names
}


make_italic_names <- function(mat, rc_fun, rc_names) {
  italic_names <- rc_fun(mat)
  ids <- rc_names %>% match(rc_fun(mat))
  ids %>%
    walk(
      function(i)
        italic_names[i] <<-
        bquote(italic(.(rc_fun(mat)[i]))) %>%
        as.expression()
    )
  italic_names
}

pheatmap(
  test,
  labels_row = make_bold_names(test, rownames, c("Gene2", "Gene5", "Gene14")))



使用两个labels_row显然是行不通的

pheatmap(
  test,
  labels_row = make_bold_names(test, rownames, c("Gene2", "Gene5", "Gene14")),
  labels_row = make_italic_names(test, rownames, c("Gene3", "Gene6", "Gene10")))

【问题讨论】:

    标签: r pheatmap


    【解决方案1】:

    显然,您不能在函数中两次传递相同的参数。

    相反,您可以编辑自定义函数,使其同时适应粗体和斜体。代码如下:

    make_face_names <- function(mat, rc_fun, rc_names_b = NA, 
                                rc_names_i = NA) {
      f_names <- rc_fun(mat)
      ids_b <- rc_names_b %>% match(rc_fun(mat))
      ids_i <- rc_names_i %>% match(rc_fun(mat))
      ids_bi <- rc_names_i %>% match(rc_fun(mat))
      
      ids_b %>%
        walk(
          function(i)
            f_names[i] <<-
            bquote(bold(.(rc_fun(mat)[i]))) %>%
            as.expression()
        )
      ids_i %>%
        walk(
          function(i)
            f_names[i] <<-
            bquote(italic(.(rc_fun(mat)[i]))) %>%
            as.expression()
        )
      
      f_names
    }
    
    
    pheatmap(test,
             labels_row = make_face_names(test,
                                          rownames, rc_names_b = c("Gene2", "Gene5", "Gene14"),
                                          rc_names_i = c("Gene3", "Gene6", "Gene10")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2021-02-26
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多