Skip to contents

phylorug compares phylogenetic trees clade by clade and shows the result as rug plots on a reference tree. plot_phylorug() draws the tree, the rug plots and the legends in one call. Many users, however, already draw their trees with ape or phytools: to colour branches, to paint regimes on the tree, to show stochastic character maps or to map a continuous trait. This tutorial shows two ways to combine those plots with phylorug rug plots:

  1. pass branch colours (and other ape::plot.phylo() options) straight to plot_phylorug();
  2. draw the tree with ape or phytools, then add the rug plots with add_rug_plots().

The data

We use the dung beetle trees that come with phylorug: one IQ-TREE backbone and four comparison analyses (two IQ-TREE, two ASTRAL).

library(phylorug)
library(ape)

trees <- read_trees(system.file("extdata", "beetles_70p", package = "phylorug"))
#> Read 5 analyses (5 trees) from: /home/runner/work/_temp/Library/phylorug/extdata/beetles_70p

outgroup <- c("NicorbUCE", "NicvesUCE")
trees <- lapply(trees, function(tr) {
  tr <- root(tr, outgroup = outgroup, resolve.root = TRUE)
  drop.tip(tr, outgroup)
})

backbone <- trees[["70p_uce"]]
others   <- trees[names(trees) != "70p_uce"]

npm <- node_presence_matrix(backbone, others,
  support_col  = 1,
  support_type = c("70p_ASTRAL_partition_entropy" = "lpp",
                   "70p_ASTRAL_uce"               = "lpp",
                   "70p_ghost"                    = "sh_alrt",
                   "70p_partition_entropy"        = "sh_alrt"))

The IQ-TREE node labels hold two values, SH-aLRT/UFBoot2. With support_col = 1 the first value, SH-aLRT, is read, so the IQ-TREE analyses are declared as “sh_alrt”; the ASTRAL trees carry local posterior probabilities (“lpp”).

1. Branch colours inside plot_phylorug()

Every argument that plot_phylorug() does not know is passed on to ape::plot.phylo(). edge.color takes one colour per branch, so a clade can be highlighted; ape::which.edge() finds the branches of a clade from its tips. Here we colour the five-species clade that contains Onthophagus s.l. laevis and O. cf. muelleri (tips STL10302Laevis and STL10298Muell). Its rug plot shows that neither ASTRAL analysis recovers this clade. The other arguments are those of the beetle figure in the main tutorial.

node  <- getMRCA(backbone, c("STL10302Laevis", "STL10298Muell"))
clade <- extract.clade(backbone, node)
cols  <- rep("black", nrow(backbone$edge))
cols[which.edge(backbone, clade$tip.label)] <- "darkorange"

plot_phylorug(
  backbone,
  npm,
  mode               = "support",
  show_support       = TRUE,
  show_support_idx   = c("1" = "sh_alrt"),
  support_label_cex  = 1,
  cell_scale         = 0.5,
  cex                = 0.7,
  legend_cex         = 2,
  palette            = "classic",
  rug_position       = "centre",
  align.tip.label    = TRUE,
  edge.color         = cols,
  edge.width         = 3
)

The same way works for edge.width, edge.lty and tip.color.

2. Draw with another tool, then add_rug_plots()

add_rug_plots() draws the rug plots on the tree that is already plotted. It reads the node positions that ape and phytools store after every plot, so the plotted tree must be the backbone used in node_presence_matrix(). It stops with a clear message if it is not.

ape

plot.phylo(backbone, edge.color = cols, edge.width = 2, cex = 0.6,
           y.lim = c(0, Ntip(backbone) + 8))      # room for the legends
add_rug_plots(npm, backbone, palette = "classic", cell_scale = 0.6, legend_cex = 2,
         legend = TRUE)

phytools: plotTree()

library(phytools)
#> Loading required package: maps

plotTree(backbone, fsize = 0.6, ftype = "i", lwd = 2,
         ylim = c(-1, Ntip(backbone) + 8))
add_rug_plots(npm, backbone, palette = "okabe_ito", cell_scale = 0.6, legend_cex = 2,
         legend = TRUE)

phytools: a painted clade with plotSimmap()

paintSubTree() paints a regime on part of the tree. The painted tree keeps the backbone’s node numbers, so the rug plots land on the right nodes.

painted <- paintSubTree(backbone, node = node, state = "B", anc.state = "A")

plotSimmap(painted, colors = c(A = "grey30", B = "darkorange"),
           fsize = 1, ftype = "i", lwd = 6,
           ylim = c(-1, Ntip(backbone) + 8))
add_rug_plots(npm, backbone, palette = "classic", cell_scale = 0.6,
              legend = TRUE, legend_cex = 2)

phytools: a continuous trait with contMap()

The trait values here are simulated, only to show the plot. setMap() recolours the map with the “Spectral” palette from base R (hcl.colors()), reversed so that it runs from blue (low) through yellow to red (high); the grey rug plots stay readable on top of it.

set.seed(1)
trait <- fastBM(backbone)
cmap  <- contMap(backbone, trait, plot = FALSE)
cmap  <- setMap(cmap, rev(hcl.colors(100, "Spectral")))

n <- Ntip(backbone)
plot(cmap, fsize = c(1, 1.5), ftype = "i", lwd = 10, sig = 2,
     legend  = 0.4 * max(nodeHeights(backbone)),
     outline = FALSE,
     leg.txt = "simulated trait",
     ylim    = c(1 - 0.12 * (n - 1), n + 8))  # room for both legends
add_rug_plots(npm, backbone, palette = "grey", cell_scale = 0.6,
              legend = TRUE, legend_cex = 2)

Which to use

You want Use
The standard phylorug figure, with legends placed for you plot_phylorug()
Branch colours, widths or line types plot_phylorug(..., edge.color = )
Painted regimes, mapped traits or any phytools plot the phytools function, then add_rug_plots()

add_rug_plots() uses the same colours, tiers and dot rules as plot_phylorug(). Its legends are optional (legend = TRUE); leave room for them at the top of the plot, for example with ylim.