【发布时间】: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