raivokolde / pheatmap

Pretty heatmaps
227 stars 83 forks source link

Bold row/column labels #48

Open skafdasschaf opened 5 years ago

skafdasschaf commented 5 years ago

I needed a heatmap with some labels highlighted in boldface. To this end, I implemented the function make_bold_names() as described below. Such a feature may be interesting for a future pheatmap release.

k/r Wolfgang

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

# use this function to make row or column names bold
# parameters:
#   mat: the matrix passed to pheatmap
#   rc_fun: either rownames or colnames
#   rc_names: vector of names that should appear in boldface
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
}

pheatmap(
  test,
  labels_row = make_bold_names(test, rownames, c("Gene2", "Gene5", "Gene14")),
  labels_col = make_bold_names(test, colnames, c("Test2", "Test3", "Test4")))

Created on 2019-01-24 by the reprex package (v0.2.1)

koopkaup commented 5 years ago

I used your function to make all rownames italic. However, I think it should be implemented to pheatmap, then it would be easier to use.

d-linnard commented 4 years ago

Thank you @skafdasschaf!

@koopkaup, how did you change it for italicized row names? Thank you!

vagabond12 commented 4 years ago

@skafdasschaf @koopkaup Then do you known how to change the color of rownames? Thank you.

patrickparts2 commented 3 years ago

@skafdasschaf This is a very nice function. as @koopkaup I have adapted your function to label some rowname in italic and I was wondering how it would be possible to adapt it a bit more to label some rownames in italic and others in bold. It would be so useful for me.

adesalegn commented 2 years ago

@skafdasschaf, Nice function! wonder if there is away to show in different color ? Best AD

dennisamne commented 1 year ago

@skafdasschaf This is a very nice function. as @koopkaup I have adapted your function to label some rowname in italic and I was wondering how it would be possible to adapt it a bit more to label some rownames in italic and others in bold. It would be so useful for me.

A bit late to the party, but with a bit of fiddling around with skafdasschaf's function I added a second vector, and for me it works on the dataset provided to add both bold and underlining, in this case:

make_bold_underline_names <- function(mat, rc_fun, rc_names, rc_names2) { bold_names <- rc_fun(mat) ids1 <- rc_names %>% match(rc_fun(mat)) ids1 %>% walk( function(i) bold_names[i] <<- bquote(bold(.(rc_fun(mat)[i]))) %>% as.expression() ) ids2 <- rc_names2 %>% match(rc_fun(mat)) ids2 %>% walk( function(i) bold_names[i] <<- bquote(underline(.(rc_fun(mat)[i]))) %>% as.expression() ) bold_names }

pheatmap( test, labels_row = make_bold_underline_names(test, rownames, c("Gene2", "Gene5", "Gene14"), c("Gene1","Gene3")), labels_col = make_bold_underline_names(test, colnames, c("Test2", "Test3", "Test4"),c("Test5", "Test7")))

mr-september commented 1 year ago

I'm getting this error: Error in rc_fun(mat) : could not find function "rc_fun".

I had to hard-code to "colnames" for my use case:

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