极树状图类似于系统发育图或者环形的聚类图,其效果如下图所示:
查了一下相关资料,可以通过以下两种方法实现。以下用mtcars数据为例。
1. ape包
library(ape)
data('mtcars')
new_mtcars <- mtcars[,1:7]
plot(as.phylo(hclust(dist(new_mtcars))),type="fan")
2. circlize和dendextend
用circlize_dendrogram画图,可以比上一种方法更精细的画图。
hc = hclust(dist(new_mtcars), method = 'complete') %>% as.dendrogram %>% set('labels_cex', c(0.8))
# 根据分支设置颜色
dend <- dend %>%
color_branches(k=4) %>%
color_labels
pdf('~/test.pdf', width=8, height = 8)
circlize_dendrogram(dend, labels_track_height = NA, dend_track_height = .4)
dev.off()
根据数据某一列分组指定颜色:
hc = hclust(dist(new_mtcars), method = 'complete') %>% as.dendrogram %>% set('labels_cex', c(0.8))
test_colors <- c('#B1F100', '#FF7400', '#FFAA00', '#1240AB', '#009999')
labels_colors(hc) = test_colors[mtcars$gear[order.dendrogram(hc)]]
pdf('~/test2.pdf', width=8, height = 8)
circlize_dendrogram(hc, labels_track_height = NA, dend_track_height = .4)
dev.off
欢迎关注!
网友评论