/
hcut.R
55 lines (54 loc) · 1.67 KB
/
hcut.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' Cut tree of consensus
#'
#' It shows dendrogram of a consensus of a tree generated by hclust.
#'
#'
#' @param consensus object consensus
#' @param h numeric scalar or vector with heights where the tree should be cut.
#' @param group an integer scalar with the desired number of group
#' @param col.text color of number consensus
#' @param cex.text size of number consensus
#' @param \dots Other parameters of the function plot() in cut()
#' @return hcut Returns a data frame with group memberships and consensus tree.
#' @author F. de Mendiburu
#' @seealso \code{\link{hclust}}, \code{\link{consensus}},
#' \code{\link{hgroups}}
#' @keywords cluster
#' @importFrom stats as.dendrogram
#' @importFrom graphics plot
#' @export
#' @examples
#'
#' library(agricolae)
#' data(pamCIP)
#' # only code
#' rownames(pamCIP)<-substr(rownames(pamCIP),1,6)
#' # groups of clusters
#' # output<-consensus(pamCIP,nboot=100)
#' # hcut(output,h=0.4,group=5,main="Group 5")
#' #
#' # hcut(output,h=0.4,group=8,type="t",edgePar=list(lty=1:2,col=2:1),main="group 8"
#' # ,col.text="blue",cex.text=1)
#'
hcut <-
function(consensus,h,group,col.text="blue",cex.text=1,...) {
dend <- consensus$dendrogram
if(class(dend)=="hclust") dend <- as.dendrogram(dend)
# cut for height
frame<-consensus$table.dend[consensus$table.dend[,4]<=h,]
cut2 <-cut(dend,h)$lower
# max clusters
max.groups<- as.numeric(length(cut2))
# cluster k
k<-group
p<-0
pclus<-rep(0,max.groups)
for(i in 1:max.groups) {
pclus[i]<-length(labels(cut2[[i]]))
}
if( k> 1) for(i in 1:(k-1))p=p+pclus[i]
#plus<-data.frame(number=pclus)
plot(cut2[[k]],...)
text(frame[,3]-p,frame[,4],labels=frame[,5],col=col.text,cex=cex.text)
return(data.frame(numbers=pclus))
}