2

I am new to R, and I am trying to construct a horizontal and vertical labelled dendrogram using dist() and hclust(). I have constructed six different types but cannot seem to add labels. Thank you if anyone has any suggestions.

I have tried many different ways to label these dendrograms without success, using as.dendrogram(), colnames(), rownames(), and label(). However, the output dendrograms have senseless labels. I am trying to label the dendrograms by "Family" - "X22", "X4", "X75", "X87". Below are the different methods which were applied, without avail.

Here is the dataframe:

  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

This code produces this matrix. However, it is not labelled

          1        2        3
 2 1.851852                  
 3 3.060077 1.208225         
 4 0.719598 2.571450 3.779675

These are my attempts to add labels

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)
4

1 回答 1

0

据我了解,您在问两个问题,我将尝试回答这两个问题:

1)如何控制 dist 对象中的项目名称?

最简单的方法是控制用于生成 dist 的 matrix/data.frame 的行名。例如:

> 
> 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 包

> 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)

我希望这有帮助。

塔尔

于 2015-06-20T09:11:42.177 回答