rpvclust

pvclust edit dendogram graph


I'm running some cluster analysis and I'm using pvclust as showed below:

d.pv <- pvclust(t(mtcars), method = "euclidean", 
            method.hclust = "complete", nboot = 10)
plot(d.pv)

I want to edit the graph and remove red, green numbers, and grey numbers. Also I want to color label on x axis according to a a specific column mtcars$cyl


Solution

  • To remove the red, green, and grey numbers use the following:

    plot(d.pv, print.num = FALSE, print.pv = FALSE)
    

    Colouring the labels is trickier within the confines of plot.pvclust. I'd suggest converting it into ggplot2 for more flexibility.

    # Run pvclust and restructure data
    d.pv <- as.dendrogram(pvclust(t(mtcars), method = "euclidean", 
                          method.hclust = "complete", nboot = 10)$hclust)
    
    ddata <- dendro_data(d.pv, type = "rectangle")
    
    # Get data frames to plot
    df_seg <- segment(ddata)
    df_labs <- data.frame(label(ddata), cyl = as.factor(mtcars[match(label(ddata)$label, rownames(mtcars)), "cyl"]))
    
    # Create ggplot dendrogram
    p <- ggplot()
    p <- p + geom_segment(data = df_seg,
                          aes(x = x, y = y, xend = xend, yend = yend),
                          size = 1.25,
                          colour = "darkgray",
                          lineend = "round")
    p <- p + geom_text(data = df_labs, 
                       aes(x = x, 
                           y = y, 
                           label = label,
                           colour = cyl), 
                       nudge_y = -10,
                       family = "serif",
                       size = 5,
                       angle = 90,
                       hjust = 1)
    p <- p + xlab("") + ylab("Height")
    p <- p + theme(axis.line.x = element_blank(),
                   axis.text.x = element_blank(),
                   axis.ticks.x = element_blank(),
                   text = element_text(family = "serif"))
    p <- p + scale_y_continuous(expand = expand_scale(add = c(85, 0)))
    p
    

    enter image description here