Search code examples
rdendrogramape-phylo

How to draw dendrogram using ape package in R?


I have a distance matrix of ~200 x 200 size I an unable to plot a dendrogram using the BioNJ option of ape library in R The size is big to make the plot visible What ways can I improve the visibilityenter image description here


Solution

  • Two options depending on your data

    If you need to calculate the distance matrix of your data then use

    set.seed(1)                          # makes random sampling with rnorm reproducible
    # example matrix
    m <- matrix(rnorm(100), nrow = 5)    # any MxN matrix
    distm <- dist(m)                     # distance matrix
    hm <- hclust(distm)
    plot(hm)
    

    If your data is a distance matrix (must be a square matrix!)

    set.seed(1)
    # example matrix
    m <- matrix(rnorm(25), nrow=5)       # must be square matrix!
    distm <- as.dist(m)
    hm <- hclust(distm)
    plot(hm)
    

    A 200 x 200 distance matrix gives me a reasonable plot

    set.seed(1)
    # example matrix
    m <- matrix(rnorm(200*200), nrow=200)       # must be square matrix!
    distm <- as.dist(m)
    hm <- hclust(distm)
    plot(hm)