Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep the annotation color unchanged (without specifying the annotation color) when I replot heatmap? #906

Closed
YonghuiDong opened this issue Apr 20, 2022 · 2 comments

Comments

@YonghuiDong
Copy link

I am using ComplexHeatmap in my shiny app to produce heatmap. Unlike pheatmap , the heatmap is perfectly rendered in shiny and it is compabible with other interactive plots generated with plotly. Thank a lot.

I am wondering if there is a way to keep the annotation color unchanged (without specifying the annotation color) upon the reactive events in Shiny?

A simple example describing my need is as following:

library(ComplexHeatmap)
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 = "")
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)

When I re-run the above code, the annotation color changes accordingly.

Thanks a lot.

Dong

@malcook
Copy link

malcook commented May 4, 2022

FWIW: I too have wished for the same outside of Shiny context when replotting a heatmap. It is unsettling when the colors change apparently for no reason.

@jokergoo
Copy link
Owner

Because if you do not set the colors explicitly, random colors will be used (the ComplexHeatmap version of pheatmap). You can either set colors by annotation_colors argument or add a fixed random seed before you run pheatmap():

set.seed(123)
pheatmap(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants