Search code examples
rggplot2dendrogramr-daisy

X axis label is not showing in clustering dendrogram in ggplot


I have done a clustering dendrogram following a previous code I found online, but the x-axis of is not being shown in the graph. I would like to have the dissimilarity value shown in the x-axis, but I have not been successful.

females<-cervidae[cervidae$Sex=="female",]
dstf   <- daisy(females[,9:14], metric = "euclidean", stand = FALSE)
hcaf   <- hclust(dstf, method = "ave")
k     <- 3
clustf <- cutree(hcaf,k=k)  # k clusters


dendrf    <- dendro_data(hcaf, type="rectangle") # convert for ggplot
clust.dff <- data.frame(label=rownames(females), cluster=factor(clustf), 
females$Genus, females$Species) 
dendrf[["labels"]]   <- merge(dendrf[["labels"]],clust.dff, by="label")
rectf <- aggregate(x~cluster,label(dendrf),range)
rectf <- data.frame(rectf$cluster,rectf$x)
ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))])


fem=ggplot() + 
   geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) + 
   geom_text(data=label(dendrf), aes(x, y, label= females.Genus, hjust=0, 
   color=females.Genus),
        size=3) +
   geom_rect(data=rectf, aes(xmin=X1-.3, xmax=X2+.3, ymin=0, ymax=ymax), 
        color="red", fill=NA)+
   coord_flip() + scale_y_reverse(expand=c(0.2, 0)) + 
   theme_dendro() + scale_color_discrete(name="Genus") + 
   theme(legend.position="none")

Here is how my dendrogram looks:

enter image description here


Solution

  • Your code included theme_dendro(), which is described in its help file as:

    Sets most of the ggplot options to blank, by returning blank theme elements for the panel grid, panel background, axis title, axis text, axis line and axis ticks.

    You force the x-axis line / text / ticks to be visible in theme():

    ggplot() + 
      geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) +
      geom_text(data=label(dendrf), aes(x, y, label= label, hjust=0,
                                        color=cluster),
                size=3) +
      geom_rect(data=rectf, aes(xmin=X1-.3, xmax=X2+.3, ymin=0, ymax=ymax),
                color="red", fill=NA)+
      coord_flip() +
      scale_y_reverse(expand=c(0.2, 0)) + 
      theme_dendro() +
      scale_color_discrete(name="Cluster") +
      theme(legend.position="none",
            axis.text.x = element_text(),  # show x-axis labels
            axis.ticks.x = element_line(), # show x-axis tick marks
            axis.line.x = element_line())  # show x-axis lines
    

    ggdendro

    (This demonstration uses a built-in dataset, since I'm not sure what's cervidae. Code used to create this is reproduced below:)

    library(cluster); library(ggdendro); library(ggplot2)
    
    hcaf   <- hclust(dist(USArrests), "ave")
    k     <- 3
    clustf <- cutree(hcaf,k=k)  # k clusters
    
    dendrf    <- dendro_data(hcaf, type="rectangle") # convert for ggplot
    clust.dff <- data.frame(label=rownames(USArrests), 
                            cluster=factor(clustf)) 
    dendrf[["labels"]]   <- merge(dendrf[["labels"]],clust.dff, by="label")
    rectf <- aggregate(x~cluster,label(dendrf),range)
    rectf <- data.frame(rectf$cluster,rectf$x)
    ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))])