简体   繁体   English

旋转叶丛树状图中的叶子标签

[英]Rotate leaf labels in pvclust dendrogram plot

I'm using the pvclust package in R to perform bootstrapped hierarchical clustering. 我在R中使用pvclust软件包执行引导式分层群集。 The output is then plotted as a hclust object with a few extra features (different default title, p-values at nodes). 然后将输出绘制为具有一些额外功能(不同的默认标题,节点的p值)的群集对象。 I've attached a link to one of the plots here. 我在这里的一个地块上附加了一个链接。

This plot is exactly what I want, except that I need the leaf labels to be displayed horizontally instead of vertically. 除了我需要叶子标签水平显示而不是垂直显示之外,此图正是我想要的。 As far as I can tell there isn't an option for rotating the leaf labels in plot.hclust. 据我所知,plot.hclust中没有旋转叶子标签的选项。 I can plot the hclust object as a dendrogram 我可以将hclust对象绘制为树状图

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

but the leaf labels are then printed in boxes that I can't seem to remove, and the heights of the nodes in the hclust object are lost. 但是叶子标签然后打印在我似乎无法删除的框中,并且hclust对象中节点的高度丢失了。 I've attached a link to the dendrogram plot here. 我在此处附加了树状图的链接。

What would be the best way to make a plot that is as similar as possible to the standard plot.pvclust() output, but with horizontal leaf labels? 制作与标准plot.pvclust()输出尽可能相似但带有水平叶子标签的图的最佳方法是什么?

One way to get the text the way you want is to have plot.dendrogram print nothing and just add the labels yourself. 一种以所需方式获取文本的方法是让plot.dendrogram打印任何内容,而只是自己添加标签。 Since you don't provide your data, I illustrate with some built-in data. 由于您不提供数据,因此我将介绍一些内置数据。 By default, the plot was not leaving enough room for the labels, so I set the ylim to allow the extra needed room. 默认情况下,该图没有为标签留出足够的空间,因此我将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)

具有水平标签的树状图

I've written a function that plots the standard pvclust plot with empty strings as leaf labels, then plots the leaf labels separately. 我编写了一个函数,该函数绘制带有空字符串的标准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])
}

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

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