The code for this function can be found here. The function has the potential to take many arguments:

To demonstrate some functionality, we first need to simulate some phylogenetic and trait data

library(ape)

phy <- read.nexus("example_phylogeny.nex")

# Create some random dstate data
states <- sample(seq(1,40,1), 17)
names(states) <- phy$tip.label

head(states)
##      Dryocopus_lineatus      Dryocopus_pileatus       Campethera_nivosa 
##                       9                      17                       7 
##   Geocolaptes_olivaceus             Picus_canus Blythipicus_rubiginosus 
##                       6                      12                      13
# Load the function code
source("color.terminal.branches.R")

A starting point plot

color.terminal.branches(phy, states, breaks=4, cols=c("black","red"), edge.width=2, show.tip.label=TRUE)

Using three colors, partitioning the data finer

color.terminal.branches(phy, states, breaks=8, cols=c("black","green","red"), edge.width=2)

If you wanted to make the non-terminal branches less obvious

color.terminal.branches(phy, states, breaks=8, cols=c("black","green","red"), edge.width=2,non.terminal.col = "gray")

If we want to color the branches using different data to calculate the colors then pass the data to the alt.col.data argument.

# Generate data which is a smaller subset of the original state data
small.range.states <- sample(seq(30,40,0.1), 17)
names(small.range.states) <- phy$tip.label

# The original state data will be passed to the 'alt.col.data' argument
color.terminal.branches(phy, small.range.states, breaks=4, cols=c("black","red"), edge.width=2,show.tip.label=TRUE, alt.col.data = states)