无法绘制树状图(无效的树状图输入错误)

Can't Plot Dendrogram (invalid dendrogram input error)

我的数据集中有 583 个观测值,我需要使用其中的 100 个来制作树状图。但是我在 plot() 函数步骤中收到以下错误。

如何troubleshoot/solve这个?

##For 100 Observations
set.seed(44)
idx_100 <- sample(1:nrow(ilpd_df), 100)
distance_matrix_100 <- dist(as.matrix(ilpd_df[idx_100,-c(1,2,10,11)]), 
                        method = "euclidean") #Creates Hierarchical Clustering Solution

hc_100 <- hclust(distance_matrix_100)
plot(hc_100, hang = -1, labels=ilpd_df$Class) #CANT PLOT DUE TO INVALID INPUT ERROR

您确定使用正确的参数来绘制您的树状图吗?因此,我在这里分享了在树状图上使用绘图函数的 S3 方法。 "hang" 未包含在此列表中。

S3 method for dendrogram

  plot(x, type = c("rectangle", "triangle"),
  center = FALSE,
  edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")),
  nodePar = NULL, edgePar = list(),
  leaflab = c("perpendicular", "textlike", "none"),
  dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s",
  horiz = FALSE, frame.plot = FALSE, xlim, ylim, …)

你能提供一些可用于重现问题的最小数据吗?

我发现 plot.hclust() 没有问题 (R 3.6.1)。

下面简单的代码

hc <- hclust(dist(USArrests)^2, "cen")
class(hc)  # [1] "hclust"
plot(hc, hang = -1, main = "Sample Tree", cex = .5)

生产