我是R的新手,我正在尝试使用dist()和hclust()构建水平和垂直标记的树形图。我已经构建了六种不同的类型但似乎无法添加标签。如果有人有任何建议,谢谢。
我尝试了许多不同的方法来使用as.dendrogram(),colnames(),rownames()和label()来标记这些树状图,但没有成功。但是,输出树形图有无意义的标签。我试图通过" Family"标记树形图。 - " X22"," X4"," X75"," X87"。以下是应用的不同方法,但无济于事。
以下是数据框:
Family SBI.CV.mean
1 X22 59.25926
2 X4 57.40741
3 X75 56.19918
4 X87 59.97886
library(dendextend)
family1$Family <- as.factor(family1$Family)
class(family1$Family)
str(family1)
family2 <- ddply(family1,.(Family), summarise,
SBI.CV.mean = mean(SBI.CV))
family2
class(family2)
par(mfrow = c(3,3))
x_dist <- dist(x=family2$SBI.CV, method="euclidean")
x_dist
class(x_dist)
x_dist <- read.table(header=T, text=c("X22", "X4", "X75", "X87"))
x_dist2=as.matrix(x_dist2, labels=TRUE,)
colnames(x_dist) <- rownames(x_dist) <- x_dist2[["X22","X4","X75","X87"]]
x_dist2
此代码生成此矩阵。但是,它没有标记
1 2 3
2 1.851852
3 3.060077 1.208225
4 0.719598 2.571450 3.779675
这些是我尝试添加标签
require(graphics)
labs=paste(c("X22", "X4", "X75", "X87"), 1:4, sep="")
x_dist2 <- x_dist
x_dist2
colnames(x_dist2) <- labs
Dendro.data <- hclust(dist(x_dist2), "euclidean")
plot(as.dendrogram(Dendro.data), horiz=T)
require(graphics)
labs=paste(c("X22", "X4", "X75", "X87"), 1:4, sep="")
x_dist3 <- x_dist
colnames(x_dist3) <- labs
Dendro.data <- hclust(dist(x_dist3), "ave")
plot(as.dendrogram(x_dist3), hang=-1)
str(Dendro.data)
hc <- hclust(dist(family2$SBI.CV), "ave")
plot(hc)
plot(as.dendrogram(hc, hang=0.02), horiz = TRUE)
dend1 <- as.dendrogram(Dendro.data)
dend1
dend1_mod_01 <- dend1
dend1_mod_01 <- colour_branches(dend1_mod_01, k=2)
col_for_labels <- c("purple","purple","orange","purple",
"orange","dark green")
dend_mod_01 <- color_labels(dend1_mod_01,col=col_for_labels)
plot(Dendro.data)
plot(dend1_mod_01)
答案 0 :(得分:0)
据我了解,您提出了两个问题,我将尝试回答两个问题:
1)如何控制dist对象中的项目名称?
最简单的方法是控制用于生成dist的matrix / data.frame的rownames。例如:
>
> x <- data.frame(value = 6:9)
> x
value
1 6
2 7
3 8
4 9
> rownames(x)
[1] "1" "2" "3" "4"
> # dist uses row names to indicate the relation between the items!
> # the default is a vector of integers, as the number of items:
> dist(x)
1 2 3
2 1
3 2 1
4 3 2 1
>
> rownames(x) <- letters[1:4]
> x
value
a 6
b 7
c 8
d 9
> rownames(x)
[1] "a" "b" "c" "d"
> # dist uses row names to indicate the relation between the items!
> # Now they are letters
> dist(x)
a b c
b 1
c 2 1
d 3 2 1
2)如何控制树形图对象中项目的名称?
为此,最好使用dendextend package:
> x <- data.frame(value = 6:9)
> x
value
1 6
2 7
3 8
4 9
> dist(x)
1 2 3
2 1
3 2 1
4 3 2 1
> hc <- hclust(dist(x))
> dend <- as.dendrogram(hc)
> plot(dend)
> # the default labels is the names in the dist:
> labels(dend)
[1] 1 2 3 4
> # Using dendextend we can update them:
> library(dendextend)
> labels(dend) <- letters[1:4]
> labels(dend)
[1] "a" "b" "c" "d"
> plot(dend)
我希望这会有所帮助。
塔尔