0

我正在使用 R 中的 pvclust 包来执行引导层次聚类。然后将输出绘制为具有一些额外特征(不同的默认标题,节点处的 p 值)的 hclust 对象。我已经附上了一个链接到这里的一个情节。

这个情节正是我想要的,除了我需要水平显示叶子标签而不是垂直显示。据我所知,在plot.hclust 中没有旋转叶子标签的选项。我可以将 hclust 对象绘制为树状图

(即plot(as.dendrogram(example$hclust), leaflab="textlike")代替plot(example)

但是叶子标签随后打印在我似乎无法删除的框中,并且 hclust 对象中节点的高度丢失了。我在这里附上了树状图的链接。

制作与标准输出尽可能相似plot.pvclust()但带有水平叶子标签的绘图的最佳方法是什么?

4

2 回答 2

1

以您想要的方式获取文本的一种方法是不plot.dendrogram打印任何内容,只需自己添加标签。由于您不提供数据,因此我用一些内置数据进行说明。默认情况下,绘图没有为标签留出足够的空间,因此我将 设置ylim为允许额外需要的空间。

set.seed(1234)
HC = hclust(dist(iris[sample(150,6),1:4]))

plot(as.dendrogram(HC), leaflab="none", ylim=c(-0.2, max(HC$height)))
text(x=seq_along(HC$labels), y=-0.2, labels=HC$labels)

带有水平标签的树状图

于 2018-03-21T13:03:07.023 回答
0

我编写了一个函数,它使用空字符串作为叶子标签绘制标准 pvclust 图,然后分别绘制叶子标签。

plot.pvclust2 <- function(clust, x_adj_val, y_adj_val, ...){
  # Assign the labels in the hclust object to x_labels,
  # then replace x$hclust$labels with empty strings.
  # The pvclust object will be plotted as usual, but without
  # any leaf labels.
  clust_labels <- clust$hclust$labels
  clust$hclust$labels <- rep("", length(clust_labels))

  clust_merge <- clust$hclust$merge #For shorter commands

  # Create empty vector for the y_heights and populate with height vals
  y_heights <- numeric(length = length(clust_labels))
  for(i in 1:nrow(clust_merge)){
    # For i-th merge
    singletons <- clust_merge[i,] < 0 #negative entries in merge indicate
                                      #agglomerations of singletons, and 
                                      #positive entries indicate agglomerations
                                      #of non-singletons.
    y_index <- - clust_merge[i, singletons]
    y_heights[y_index] <- clust$hclust$height[i] - y_adj_val
  }

  # Horizontal text can be cutoff by the margins, so the x_adjust moves values
  # on the left of a cluster to the right, and values on the right of a cluster
  # are moved to the left
  x_adjust <- numeric(length = length(clust_labels))
  # Values in column 1 of clust_merge are on the left of a cluster, column 2
  # holds the right-hand values
  x_adjust[-clust_merge[clust_merge[ ,1] < 0, 1]] <- 1 * x_adj_val
  x_adjust[-clust_merge[clust_merge[ ,2] < 0, 2]] <- -1 * x_adj_val

  # Plot the pvclust object with empty labels, then plot horizontal labels
  plot(clust, ...)
  text(x = seq(1, length(clust_labels)) +
         x_adjust[clust$hclust$order],
       y = y_heights[clust$hclust$order],
       labels = clust_labels[clust$hclust$order])
}
于 2018-03-21T17:06:27.840 回答