rplotdendrogramhclustpvclust

Rotate leaf labels in pvclust dendrogram plot


I'm using the pvclust package in R to perform bootstrapped hierarchical clustering. The output is then plotted as a hclust object with a few extra features (different default title, p-values at nodes). 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. I can plot the hclust object as a dendrogram

(i.e. plot(as.dendrogram(example$hclust), leaflab="textlike") instead of 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. 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?


Solution

  • I've written a function that plots the standard pvclust plot with empty strings as leaf labels, then plots the leaf labels separately.

    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])
    }