pheatmap r-注释传说未显示所有值

发布于 2025-01-27 17:42:16 字数 755 浏览 1 评论 0原文

绘制注释时,并非所有簇显示(8中只有4个)。我如何解决它的名字并查看所有颜色? 谢谢!

数据是一个相关图(COR_MARTIX),我提取了由pheatmap创建的簇,并从中提取注释。

代码:

chosen_k = 4 
res = pheatmap(mat = cor_martix, breaks=seq(-1, 1, length.out=101))

myannotation = as.data.frame(cutree(res$tree_row,k = chosen_k)) #extract k clusters
names(myannotation)[1] = "cluster" 
palette1 <- rainbow(chosen_k,alpha = 0) #make colors
ann_colors = list (cluster = palette1) #make annotation colors
pheatmap(mat = cor_martix, breaks=seq(-1, 1, length.out=101),annotation_col =  myannotation, annotation_colors =ann_colors)

when drawing annotation, not all of the clusters are shown (only 4 out of 8). How can I solve it and see all the colors with their names?
Thanks!
enter image description here

The data is a correlation map (cor_martix), that I extracted the clusters that created by pheatmap, and make an annotation from them.

code:

chosen_k = 4 
res = pheatmap(mat = cor_martix, breaks=seq(-1, 1, length.out=101))

myannotation = as.data.frame(cutree(res$tree_row,k = chosen_k)) #extract k clusters
names(myannotation)[1] = "cluster" 
palette1 <- rainbow(chosen_k,alpha = 0) #make colors
ann_colors = list (cluster = palette1) #make annotation colors
pheatmap(mat = cor_martix, breaks=seq(-1, 1, length.out=101),annotation_col =  myannotation, annotation_colors =ann_colors)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2025-02-03 17:42:16

这是解决您问题的解决方案。

library(pheatmap)
chosen_k = 8 

# Generate correlation matrix
X <- matrix(rnorm(100*28), nrow=100)
colnames(X) <- paste0("x",1:ncol(X))  
cor_matrix <- cor(X)

首先,当生成myannotation数据框架时,cluster列必须定义为一个因素。

myannotation <- as.data.frame(cutree(res$tree_row, k = chosen_k))
names(myannotation)[1] = "cluster" 
myannotation$cluster <- factor(myannotation$cluster, levels=1:chosen_k, 
                               labels=paste0("Clu", 1:chosen_k))

然后,当定义带有注释颜色的ann_colors列表时,每种颜色必须与相应的群集名称关联。

palette1 <- rainbow(chosen_k, alpha=.5) #make colors
names(palette1) <- levels(myannotation$cluster)   
ann_colors <- list(cluster = palette1)

现在,注释传说显示了正确数量的颜色。

pheatmap(mat = cor_matrix, breaks=seq(-1, 1, length.out=101), 
         annotation_col=myannotation, annotation_colors=ann_colors)

Here is the solution to your problem.

library(pheatmap)
chosen_k = 8 

# Generate correlation matrix
X <- matrix(rnorm(100*28), nrow=100)
colnames(X) <- paste0("x",1:ncol(X))  
cor_matrix <- cor(X)

First, when generating the myannotation data frame, the cluster column must be defined as a factor.

myannotation <- as.data.frame(cutree(res$tree_row, k = chosen_k))
names(myannotation)[1] = "cluster" 
myannotation$cluster <- factor(myannotation$cluster, levels=1:chosen_k, 
                               labels=paste0("Clu", 1:chosen_k))

Then, when defining the ann_colors list with annotation colors, each color must be associated to the corresponding cluster name.

palette1 <- rainbow(chosen_k, alpha=.5) #make colors
names(palette1) <- levels(myannotation$cluster)   
ann_colors <- list(cluster = palette1)

Now the annotation legend shows the right number of colors.

pheatmap(mat = cor_matrix, breaks=seq(-1, 1, length.out=101), 
         annotation_col=myannotation, annotation_colors=ann_colors)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文