hclust 和 ggplot r

hclust and ggplot r

我有一个文件,其值如下所示

gene_name s1    s2  s3  s4  s5
gene1  0.5004357 -0.9613324  1.4624021 -0.8051191 -0.1963863
gene2  1.1662839 -0.3210387 -0.3653730 -1.3095341  0.8296619
gene3  1.0511340 -0.7007560 -0.3025992  1.0511340 -1.0989128
gene4 -0.2422484 -0.4203723  0.4651577 -1.2295635  1.4270265
gene5 -1.3491928 -0.6743735  0.1860456  0.9507387  0.8867820
gene6 -0.9254673  0.1860328 -1.0089603  0.3438866  1.4045082
dim(df)
[1] 21752     5

我想要实现的是使用 hclust 和 dist 方法来查看数据趋势,我试图基本上做问题中显示的事情 p.s sandipan dey 的回答

问题中没有显示数据,我无法理解,我想绘制的是

  1. x: xaxis 我的样本名称 (s1,s2,s3,s4,s5)

  2. y 轴 zscore 和

  3. 每行代表每个基因名

  4. facet_wrap 对于每个集群,我可以看到哪个集群可以很好地清晰地聚类或分离样本

编辑

基于回答

我的代码版本

d_final <- cbind.data.frame(expr, cluster=cutree(hc, k = n))
d_final %>% 
  gather(key, value, -geneID, -cluster) %>% 
  ggplot(aes(x=key, y=value, color=factor(cluster), group=geneID)) + 
  geom_point() + geom_path() +
  facet_wrap(~cluster) #changed it to wrap

当我尝试这个时

d <- dist(expr[,-1] , method = "euclidean")
hc <- hclust(dist(d), method = "average")

在具有 16gb 内存的 mac 上 R studio 冻结

是这样的吗?

library(tidyverse)
hc <- hclust(dist(d[,-1]))
plot(hc)
# try three clusters for instance: 
n <- 3
d_final <- cbind.data.frame(d, cluster=cutree(hc, k = n))
d_final %>% 
  gather(key, value, -gene_name, -cluster) %>% 
  ggplot(aes(x=key, y=value, color=factor(cluster), group=gene_name)) + 
     geom_point() + geom_path() +
     coord_flip() +
     facet_grid(~gene_name)

 # or change to
    facet_grid(~cluster)