r - Labelling Vertical and Horizontal Dendrograms -
i new r, , trying construct horizontal , vertical labelled dendrogram using dist() , hclust(). have constructed 6 different types cannot seem add labels. thank if has suggestions.
i have tried many different ways label these dendrograms without success, using as.dendrogram(), colnames(), rownames(), , label(). however, output dendrograms have senseless labels. trying label dendrograms "family" - "x22", "x4", "x75", "x87". below different methods applied, without avail.
here 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 matrix. however, not labelled
1 2 3 2 1.851852 3 3.060077 1.208225 4 0.719598 2.571450 3.779675 these attempts 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)
as far understand, asking 2 questions, , i'll try answer both:
1) how control names of items in dist object?
the easiest way control rownames of matrix/data.frame used produce dist. example:
> > 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 indicate relation between items! > # default vector of integers, number of items: > dist(x) 1 2 3 2 1 3 2 1 4 3 2 1 > > rownames(x) <- letters[1:4] > x value 6 b 7 c 8 d 9 > rownames(x) [1] "a" "b" "c" "d" > # dist uses row names indicate relation between items! > # letters > dist(x) b c b 1 c 2 1 d 3 2 1 2) how control names of items in dendrogram object?
for best use 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) > # default labels names in dist: > labels(dend) [1] 1 2 3 4 > # using dendextend can update them: > library(dendextend) > labels(dend) <- letters[1:4] > labels(dend) [1] "a" "b" "c" "d" > plot(dend) i hope helps.
tal
Comments
Post a Comment