我在树状图中为叶子着色如下
require(graphics)
dm <- hclust(dist(USArrests[1:5,]), "ave")
df<-data.frame("State"=c("Alabama","Alaska","Arizona","Arkansas","California"), "Location"=c("South","North","West","South","West"))
color.sites<-function(dm){
dend<-as.dendrogram(dm)
plot(dend)
cols <- attributes(dend)
df$ColorGroups <- factor(df$Location)
#Set colour pallette
Location.Pal <- rainbow(nlevels(df$ColorGroups), s=0.9,v=0.9,start=0.1,end=0.9,alpha=1)
colorleaves <- function (n) {
# only apply to "leaves" in other words the labels
if(is.leaf(n)) {
i <- which(df$State == attr(n,"label"))
col.lab <- Location.Pal[[unclass(df$ColorGroups[[i]])]]
a <- attributes(n)
attr(n, "nodePar") <- c(a$nodePar, list(lab.col = col.lab))
}
n
}
xx <- dendrapply(dend, colorleaves)
plot(xx, cex=3, cex.main=2, cex.lab=5, cex.axis=1, mar=c(3,3,3,3), main="Title")
}
color.sites(dm)
我想:1)添加一个解释颜色的图例(即橙色 = 北)2)使叶子标签更大更粗(cex.lab 似乎没有做这项工作)3)创建一个具有鲜明对比的调色板当树状图中有许多叶子和颜色时,颜色(彩虹,heat.colors 等似乎都融合在一起了。
任何意见是极大的赞赏 !