Heatmaply侧边栏颜色,如何正确显示指定颜色?

Heatmaply sidebar colors, how to show specified colors correctly?

我有问题,可能是 heatmaply 或 plotly 中的错误。热图侧边栏中的颜色未显示我指定的颜色。请参阅下面的代码示例,在第 6 部分代码的末尾,使用 plot 函数绘制的第一个图(显示颜色的简单图)正确显示了颜色(黄色和蓝色):

在 heatmaply 侧栏中使用这些颜色的第二个图(heatmamply 侧栏颜色错误):

无法正确显示它们,而是显示随机颜色。在具有真实数据的类似图中,侧边栏中甚至有红色和橙色(heatmaply 侧边栏显示红色和橙色,而颜色范围为蓝黄色):

而所有代码都是使用蓝黄色范围生成的。任何可能导致此错误的想法以及如何在侧边栏中显示与其颜色代码 a 一致的颜色?

基于完整数据和数据子样本比较两棵树中叶子之间的共生相似性

# 1 ) Generate random data to build trees
set.seed(2015-04-26)
dat <- matrix(rnorm(100), 10, 50) # Dataframe with 50 columns
datSubSample <- dat[, sample(ncol(dat), 30)] #Dataframe with 30 columns sampled from the dataframe with 50
dat_dist1 <- dist(datSubSample)
dat_dist2 <- dist(dat)
hc1 <- hclust(dat_dist1)
hc2 <- hclust(dat_dist2)

# 2) Build two dendrograms, one based on all data, second based a sample of the data (30 out of 50 columns)
dendrogram1 <- as.dendrogram(hc1)
dendrogram2 <- as.dendrogram(hc2)

# 3) For each leave in a tree get cophenetic distance matrix, 
# each column represent distance of that leave to all others in the same tree
cophDistanceMatrix1 <- as.data.frame(as.matrix(cophenetic(dendrogram1)))
cophDistanceMatrix2 <- as.data.frame(as.matrix(cophenetic(dendrogram2)))

# 4) Calculate correlation between cophenetic distance of a leave to all other leaves, between two trees
corPerLeave <- NULL # Vector to store correlations for each leave in two trees
for (leave in colnames(cophDistanceMatrix1)){
  cor <- cor(cophDistanceMatrix2[leave], cophDistanceMatrix1[leave])
  corPerLeave <- c(corPerLeave, unname(cor))
}

# 5) Convert cophenetic correlation to color to show in side bar of a heatmap
corPerLeave <- corPerLeave / max(corPerLeave) #Scale 0 to 1 correlation
byPal <- colorRampPalette(c('yellow', 'blue')) #blue yellow color palette, low correlation = yellow
colCopheneticCor <- byPal(20)[as.numeric(cut(corPerLeave, breaks =20))]

# 6) Plot heatmap with dendrogram with side bar that shows cophenetic correlation for each leave 
row_dend  <- dendrogram2
x  <- as.matrix(dat_dist2)
#### Plot belows use the same color code, normal plot works, however heatmaply shows wrong colors
plot(x = 1:length(colCopheneticCor), y = 1:length(colCopheneticCor), col = colCopheneticCor)
heatmaply(x, colD = row_dend, row_side_colors = colCopheneticCor)

找到解决方案,您可以在 row_side_palette 参数中使用带有热图构建颜色的函数。最小的示例代码,可以与问题本身中的代码结合使用,以在侧边栏中以不同颜色表示的每个 leave/species 显示具有共生距离的热图:

ByPal <- colorRampPalette(c('red','blue')) # Bi color palette function to be used in sidebar 

heatmaply(m,colD = row_dend, file=fileName1, plot_method= "plotly",colorscale='Viridis',row_side_palette= byPal ,
                row_side_colors=data.frame("Correlation cophenetic distances" = corPerLeave, check.names=FALSE))

我还没有解决的一个问题是如何在图例中显示连续的颜色条,有什么建议吗?