In a current project, I have a need to show boxplots showing distributions of values at some (not all) internal nodes in a phylogeny. The function plotTree.boxplot already exists in the wonderful package phytools for showing boxplots next to tips, here I present the function boxplot.by.node.

source("boxplot.by.node.R")

The function can be downloaded here. boxplot.by.node takes the following arguments:

The function requires phytools to be installed

library(phytools)
## Loading required package: ape
## Loading required package: maps

To demonstrate the function, let’s generate some dummy data.

# Not required for the function to run, but used here to generate an example tree
library(diversitree) 

# for replication
set.seed(66)
phy <- tree.bd(pars=c(0.3,0.01), max.taxa = 20)

plot(phy)
nodelabels()

Let’s say we have data for nodes 39, 32, and 22 that we would like to visualize such that we want the boxplot to be arranged in that order top to bottom. This is easy to do if we have a manageable number of nodes, but automating the procedure will undoubtably by a time-saver for all those lovely large phylogenies out there. So, let’s generate some dummy data and check out the function. Note, this function requires that the column names are named as presented here (node_[node number]). For large data sets, this will need to be considered upstream in any workflows.

source("boxplot.by.node.R")

# Data is currently out of order
my.plot.dat <- data.frame(node_22 = rnorm(n = 10, mean = 1, sd = 1),
                          node_39 = rnorm(n = 10, mean = 5, sd = 2),
                          node_32 = rnorm(n = 10, mean = 3, sd = 1.5))

boxplot.by.node(phy, my.plot.dat, label.nodes = TRUE, node.cols = "red", color.boxplot = FALSE)

Alternatively, let’s color code each of the individual nodes and corresponding boxplots.

boxplot.by.node(phy, my.plot.dat, label.nodes = TRUE, 
                node.cols = c("#009E73", "#e79f00", "#9ad0f3"),
                color.boxplot = TRUE)

The function allows extra arguments to be passed to boxplot. For example, let’s label our axes.

boxplot.by.node(phy, my.plot.dat, label.nodes = TRUE, 
                node.cols = c("#009E73", "#e79f00", "#9ad0f3"),
                color.boxplot = TRUE, xlab="trait val", ylab=" node")