Plotting traits on phylogenetic tree in R - issue with aligning traits
0
0
Entering edit mode
8.9 years ago
ch_at ▴ 10

Hello,

I am currently trying to prepare a phylogenetic tree for a poster - what I want to do is map three traits to each tip label. I want one column for each trait with different colours. I have everything figured out, I have the legend, the headings for each column, I have the exact order of the tip labels associated with the right traits, but I have problems plotting it in a nice way in the graph.

I used a plot.phylo2 command to plot the tree so that the tip labels are nicely aligned on the right side, because otherwise the tip labels are associated with the end of the tree branches and this doesn't look nice. However, when I map the traits - it places them right at the end of the branches, so they are in no way aligned and I don't know how to align them nicely in a column. Using the adj command doesn't help, because it just moves them to the right or left, without aligning them in one column.

Is there anyone, who could help me out with this?

Tree file:

(P_fluorescens:0.143294,((Ps_actinidiae_J30:0.009843999999999992,Ps_tomato:0.005957999999999991):0.014249000000000012,(K36B-III:0.019223000000000004,(R15C-I:0.005366999999999997,(R13A-I:7.100000000000162E-5,R14C-I:0.0021609999999999963):0.008161000000000002):0.011004):0.03967):0.0);

Traits:

id    host
P_fluorescens    red
Ps_actinidiae_J30    green
Ps_tomato    red
K36B-III    green
R13A-I    red
R14C-I    green
​

R-script

library(ape)
library(geiger)
library(phytools)

###########################################################################
#Load tree
###########################################################################
treefile<-"R-sampletree.newick"
read.newick(treefile) -> tree 
summary(tree)
tree<-root(tree, "P_fluorescens")
tree <- collapse.singles(tree)
ladderize(tree, right=TRUE) -> lad_tree

lad_tree$edge[,2]->nodes 
nodes[nodes<=length(lad_tree$tip.label)] -> tip.order
lad_tree$tip.label[tip.order] -> tip.label.order

###########################################################################
# Load trait data
###########################################################################
read.csv("traits-2.csv") -> unsorted.traitsData
data.frame(unsorted.traitsData[,2:4]) -> unsorted.traits
unsorted.traitsData[,1] -> rownames(unsorted.traits) 
unsorted.traits[tip.label.order,] -> traits

host<-traits[,1]
names(host)<-row.names(traits)
host

###########################################################################
#Plot a tree with dots connecting to the tip labels aligned on the left
###########################################################################

plot.phylo2 <- plot.phylo
environment(plot.phylo2) <- environment(plot.phylo)

body(plot.phylo2)[[c(34, 3, 6, 3, 4, 3)]] <- quote({
  mx <- max(xx[1:Ntip])
  segments(xx[1:Ntip], yy[1:Ntip] + loy, mx, yy[1:Ntip] + loy, 
           lty=2, col="white")
  text(mx + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj, 
       font = font, srt = srt, cex = cex, col = tip.color)
})

###########################################################################
#Plot the trait data onto the tree
###########################################################################

host_colour<-rep("white", length(host))
names(host_colour)<-row.names(traits)
host_colour[host==""]<-"white"
host_colour[host=="red"]<-"darkgoldenrod1"
host_colour[host=="green"]<-"springgreen4"

par(mar=c(8,2,2,2), oma=c(2,2,2,2))

pdf("Tree.pdf", width=15, height=100)

plot.phylo2(lad_tree, label.offset=0.1, cex=0.95, font=1)
title(main="Tree", cex.main=1,line=3)
add.scale.bar(cex=0.5) 

# Add the traits 
tiplabels(pch=22, cex=1.1, bg=host_colour[lad_tree$tip.label])

dev.off()
traits graphics R phylogenetic-tree • 5.3k views
ADD COMMENT
0
Entering edit mode

I am not expert of phytools. But I love using ggtree. Worth trying this for tree visualisation in R

ADD REPLY

Login before adding your answer.

Traffic: 1483 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6