【问题标题】:Turning a Presence/Absence Matrix into a Cluster Analysis in R Studio在 R Studio 中将存在/不存在矩阵转换为聚类分析
【发布时间】:2018-08-06 20:30:14
【问题描述】:

好的,我有一个包含 6 个样本的存在/不存在矩阵,其中包含 25 种存在/不存在的可能性。

我已经能够使用数据制作聚类树状图,但我宁愿将其绘制为看起来更好且更易于分析的距离矩阵? (也许是集群图或类似的东西?)

我真的很想弄清楚下一部分 - 我已经花了几天时间在这里搜索和各种其他 Google 搜索,但什么都没有出现!

这是集群树状图的代码:

matrix<-read.csv("Horizontal.csv")
distance<-dist(matrix)
hc.m<-hclust(distance)
plot(hc.m, labels=matrix$Sample, main ="", cex.main=0.8, cex.lab= 1.1)

救命!

> dput(head(matrix,20))structure(list(Sample = structure(1:6, .Label =     c("CL1", "CL2", 
"CL3", "COL1", "COL2", "COL3"), class = "factor"), X = c(0L, 
0L, 0L, 1L, 1L, 1L), X.1 = c(1L, 0L, 0L, 1L, 1L, 1L), X.2 = c(1L, 
1L, 1L, 0L, 0L, 0L), X.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X.4 = c(1L, 
1L, 1L, 0L, 0L, 0L), X.5 = c(0L, 0L, 0L, 1L, 1L, 0L), X.6 = c(1L, 
1L, 1L, 1L, 1L, 1L), X.7 = c(1L, 1L, 1L, 1L, 1L, 1L), X.8 = c(0L, 
0L, 0L, 1L, 1L, 1L), X.9 = c(0L, 0L, 0L, 1L, 1L, 1L), X.10 = c(1L, 
1L, 1L, 1L, 1L, 1L), X.11 = c(1L, 1L, 1L, 1L, 1L, 1L), X.12 = c(1L, 
1L, 1L, 1L, 1L, 1L), X.13 = c(1L, 0L, 0L, 0L, 0L, 0L), X.14 = c(0L, 
0L, 0L, 1L, 1L, 1L), X.15 = c(0L, 0L, 0L, 1L, 1L, 1L), X.16 = c(1L, 
1L, 1L, 1L, 0L, 0L), X.17 = c(1L, 1L, 1L, 1L, 1L, 1L), X.18 = c(1L, 
1L, 1L, 1L, 1L, 1L), X.19 = c(1L, 1L, 1L, 1L, 1L, 1L), X.20 = c(1L, 
1L, 1L, 1L, 1L, 1L), X.21 = c(1L, 1L, 1L, 1L, 0L, 0L), X.22 = c(0L, 
0L, 0L, 0L, 1L, 1L), X.23 = c(1L, 1L, 1L, 1L, 1L, 1L), X.24 = c(0L, 
1L, 1L, 1L, 1L, 1L)), .Names = c("Sample", "X", "X.1", "X.2", 
"X.3", "X.4", "X.5", "X.6", "X.7", "X.8", "X.9", "X.10", "X.11", 
"X.12", "X.13", "X.14", "X.15", "X.16", "X.17", "X.18", "X.19", 
"X.20", "X.21", "X.22", "X.23", "X.24"), row.names = c(NA, 6L
), class = "data.frame")

这个代码没问题:

library(vegan)
library(ggplot2)
library(tidyverse)
library(MASS)
#set working directory
setwd("~/Documents/Masters/BS707/Metagenomics")
#read csv file
cookie<-read.csv("Horizontal.csv")
data.frame(cookie, row.names = c("CL1", "CL2", "CL3", "COL1", "COL2", "COL3"))
df = subset(cookie)
data.frame(df, row.names = c("CL1", "CL2", "CL3", "COL1", "COL2", "COL3"))
dm<- dist(df, method = "binary")  #calculate the distance matrix
cmdscale(dm, eig = TRUE, k=2) -> mds
as.tibble(mds$points)  #mds coordinates
bind_cols(df, Sample = df$Sample)  #bind sample names  
mutate(df,group = gsub("\\d$", "", "Sample1"))#remove last digit from   sample names to form groups
ggplot(df)+
 geom_point (aes(x = "V1",y = "V2", color = "group")) #plot
as.tibble(mds$points) %>% ggplot() + geom_point (aes(x = V1, y = V2))

我得到了情节,但每个组都被命名为“样本”而不是 CL1、CL2、CL3、COL1、COL2、COL3。我不得不删除 %>% 因为我的 R 没有将它识别为命令或任何东西,并且每次都给出错误(切换到 + 或删除,然后它工作正常)。

【问题讨论】:

  • 你能提供你的情节是什么样子吗?
  • 有没有办法绘制它,以便您沿轴(类似于 NMDS 图的单位)与标记为单个点的 6 个样本有相似之处?我真的很难找出呈现它的最佳方式。对不起,如果那没有帮助!
  • 也许欧几里得距离不是计算二进制数据距离的最佳方法。试试dist(matrix, method="binary"),它将计算 Jaccard 距离。如果您想在二维中绘制距离矩阵,请查看 NMDS,例如 library(vegan) metaMDS
  • @missuse 谢谢!!我去看看:)
  • @EmilyDelva 没有人喜欢根据图像键入文本。请使用命令dput(head(matrix, 20)) 并将结果粘贴到帖子中。

标签: r cluster-analysis dendrogram


【解决方案1】:

这是一种在二维中可视化数据的方法:

library(tidyverse)

df %>%
  dplyr::select(-1) %>% #remove first column
  dist(method = "binary") %>% #calculate the distance matrix
  cmdscale(eig = TRUE, k = 2) -> mds #do MDS also known as principal coordinates analysis

as.tibble(mds$points) %>% #mds coordinates
  bind_cols( Sample = df$Sample) %>% #bind sample names
  mutate(group = gsub("\\d$", "", Sample)) %>% #remove last digit from sample names to form groups
  ggplot()+
  geom_point(aes(x = V1,y = V2, color = group)) #plot

或者没有tidyverse:

df_dist <- dist(df[,-1], method = "binary") 
mds <- cmdscale(df_dist, eig = TRUE, k = 2) 

for_plot <- data.frame(mds$points, group = gsub("\\d$", "", df$Sample))

ggplot(for_plot)+
  geom_point(aes(x = X1,y = X2, color = group))

其他选项包括使用来自MASS 库的isoMDS 将执行Kruskal 的非度量多维缩放或来自vegan 库的metaMDS 使用来自随机开始、轴缩放和物种分数的稳定解进行非度量多维缩放.

【讨论】:

  • 现在在家里尝试这个,我遇到了两个我不明白的错误 - 一个是“UseMethod中的错误(“select_”):没有适用于'select_'的方法应用于类“函数”的对象-假设在这里我需要更改数据的类?然后是“as.tibble(mds$points) 中的错误:找不到对象‘mds’”——我是否错过了为此安装软件包?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2018-11-03
  • 2020-08-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
相关资源
最近更新 更多