Tuesday, June 11, 2024

Adjusting clade label colors in phytools using par

Earlier today, I received the following email:

“The clade labelelling (cladelabels) and color of a particular clade is not responding as it used to in older versions of phytools and R. Currently, in the new version of phytools 2.0.0 and above when I tried to color and indicate the particular clade in the tree with vertical coloured bar or line it failed to give coloured vertical bar or line (expecting coloured but I got black clade label vertical bar).”

Since it’s hard to keep track of all the functionality of the package, I first assumed this was accurate.

Upon further investigation, however, I can find no evidence that phytools::cladelabels ever allowed direct user control of the label color. (If I’m mistaken, please correct me, of course!)

If one wants to plot clade labels using different colors, this is possible – but one needs to do so with par.

Here’s a quick demo using code rescued from the deep in the phytools archive.

set.seed(7)
library(phytools)
## this is just code to get a "realistic" looking tree
tree<-pbtree(n=26,tip.label=
    paste(LETTERS,"._",sapply(round(runif(n=26,min=3,max=6)),
      function(x) paste(sample(letters,x),collapse="")),sep=""),
  scale=1)
tree$edge.length<-tree$edge.length+0.1*rchisq(n=
    length(tree$edge.length),df=10)/10
plotTree(tree,ftype="i") ## also can use plot.phylo
nodelabels()

plot of chunk unnamed-chunk-1

Let’s label the clades descended from nodes 28, 33, 45, and 49.

plotTree(tree,xlim=c(0,1.25*max(nodeHeights(tree))),
  ftype="i")
cladelabels(tree,node=49,"clade D")
cladelabels(tree,node=45,"clade C")
cladelabels(tree,node=33,"clade B")
cladelabels(tree,node=28,"clade A")

plot of chunk unnamed-chunk-2

As I mentioned before, there’s no way to adjust the color of the clade label lines directly, but we can modify them using par. The same happens to be true for lwd, so I’m going to show both at once.

plotTree(tree,xlim=c(0,1.25*max(nodeHeights(tree))),
  ftype="i")
par(fg="red",lwd=3)
cladelabels(tree,node=49,"clade D")
par(fg="blue",lwd=3)
cladelabels(tree,node=45,"clade C")
par(fg="orange",lwd=3)
cladelabels(tree,node=33,"clade B")
par(fg="darkgreen",lwd=3)
cladelabels(tree,node=28,"clade A")

plot of chunk unnamed-chunk-3

par(fg="black",lwd=1)

Note that the text of the labels did not change – to adjust that, we would use par()$col.lab.

That’s all, folks!

No comments:

Post a Comment

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.