简体   繁体   English

如何使用 R 获得好的树状图

[英]How to get a good dendrogram using R

I am using R to do a hierarchical cluster analysis using the Ward's squared euclidean distance.我正在使用 R 使用 Ward 的平方欧几里德距离进行层次聚类分析。 I have a matrix of x columns(stations) and y rows(numbers in float), the first row contain the header(stations' names).我有一个 x 列(站)和 y 行(浮点数)的矩阵,第一行包含标题(站名)。 I want to have a good dendrogram where the name of the station appear at the bottom of the tree as i am not able to interprete my result.我想要一个很好的树状图,其中车站的名称出现在树的底部,因为我无法解释我的结果。 My aim is to find those stations which are similar.我的目标是找到那些相似的电台。 However using the following codes i am having numbers (100,101,102,...) for the lower branches.但是,使用以下代码,我得到了较低分支的编号 (100,101,102,...)。

Yu<-read.table("yu_s.txt",header = T, dec=",")
library(cluster)
agn1 <- agnes(Yu, metric = "euclidean", method="ward", stand = TRUE)
hcd<-as.dendrogram(agn1)

par(mfrow=c(3,1))

plot(hcd, main="Main")
plot(cut(hcd, h=25)$upper, 
     main="Upper tree of cut at h=25")
plot(cut(hcd, h=25)$lower[[2]], 
     main="Second branch of lower tree with cut at h=25")

A nice collection of examples are present here ( http://gastonsanchez.com/blog/how-to/2012/10/03/Dendrograms.html )此处提供了大量示例( http://gastonsanchez.com/blog/how-to/2012/10/03/Dendrograms.html

Two methods:两种方法:

with hclust from base R来自基础R hclust

hc<-hclust(dist(mtcars),method="ward")
plot(hc)

Default plot默认绘图

在此处输入图片说明

ggplot绘图

with ggplot and ggdendro使用ggplotggdendro

library(ggplot2)
library(ggdendro)

# basic option
ggdendrogram(hc, rotate = TRUE, size = 4, theme_dendro = FALSE)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM