-1

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

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

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

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

在完整数据和数据子样本的基础上比较两棵树中叶子之间的共同相似性

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

1 回答 1

1

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

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

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

于 2018-09-10T10:48:46.990 回答