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
|
plot_dendro <- function(x, title="", labels.colname=NULL, colors.colname=NULL) {
require(ggdendro)
meta.x <- pData(x)
meta.x <- as.matrix(meta.x)
myDist <- dist(t(exprs(x)))
myTree <-hclust(myDist)
dhc <- as.dendrogram(myTree)
ddata <- dendro_data(dhc, type="rectangle")
if (identical(colnames(exprs(x)), row.names(meta.x))) {
meta.x <- row2colnames(meta.x, "rownames")
matchcol <- "rownames"
} else if (any(apply(meta.x, 2, function(column) identical(as.character(unlist(column)), colnames(exprs(x)))))) {
matchcol <- names(which(apply(meta.x, 2, function(column) identical(as.character(unlist(column)), colnames(exprs(x))))))
} else {
print("ExpressionSet sampleNames and pData row.names or pData column must match")
stop()
}
ddata$labels <- merge(ddata$labels, meta.x, by.x="label", by.y=matchcol)
ggplot(segment(ddata)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
theme_dendro() +
geom_text(data=label(ddata), aes_string(x='x', y='y', label=labels.colname, color=colors.colname, hjust=-0.1), size=6)+
scale_color_brewer(type = "seq", palette = "Set1")+
coord_flip() + scale_y_reverse(expand=c(0.2, 50)) +
theme(axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank()) +
ggtitle(title)
}
|