Dustin
Dustin

Reputation: 323

In R when coloring a phylogenetic tree by a trait using tip.color(), how do you choose colors and add the legend?

I have a tree that looks like this;

# Create tree
library(phytools)
sim.tree<-pbtree(n=18)
plot(sim.tree)

I have a data frame with my trait (a vector of class factor) to color like this;

# data frame with vector to color tree with
df<-data.frame(tip = paste0("t", 1:18),
               vector.to.color.with = as.factor(c("<10", "10-20", "10-20", "10-20", "NA", "10-20", 
                                   "10-20", "10-20", "20-35", "<10", "10-20", "<10", 
                                   "35", "20-35", "<10", "NA", "10-20", "<10")))

With some great assistance from SO I was able to color the tip labels like this;

cols <- as.integer(df$vector.to.color.with[match(sim.tree$tip.label,df$tip)])
plot(sim.tree, tip.color = cols)

I am now wanting to choose my own colors, plus add a legend. Any help is greatly appreciated.

Upvotes: 0

Views: 1099

Answers (1)

Stefano Barbi
Stefano Barbi

Reputation: 3194

Here is one version with custom colors:

library(dplyr)
library(phytools)
## library(RColorBrewer)

# Create tree
df<-data.frame(tip = paste0("t", 1:18),
               brk = as.factor(c("<10", "10-20", "10-20", "10-20", "NA", "10-20", 
                                 "10-20", "10-20", "20-35", "<10", "10-20", "<10", 
                                 "35", "20-35", "<10", "NA", "10-20", "<10"))) |>
  mutate(color = recode(brk,
                        "<10"   = "red",
                        "10-20" = "green",
                        "NA"    = "gray",
                        "20-35" = "blue",
                        "35"    = "black"))
  ## mutate(color = brewer.pal(6, "Set1")[brk])


set.seed(123)
sim.tree<-pbtree(n=18)
cols <- with(df, as.character(color[match(sim.tree$tip.label, tip)]))


plot(sim.tree, tip.color = cols, x.lim = c(0,3))


with(df,
     legend(x=2.5, y=10,
            legend = levels(brk),
            ## col = brewer.pal(6, "Set1"),
            col = as.character(levels(color)),
            pch = 20,
            box.lwd = 1))

enter image description here

And here a solution with RColorBrewer which provides already good-looking palettes.

library(dplyr)
library(phytools)
library(RColorBrewer)

# Create tree
df<-data.frame(tip = paste0("t", 1:18),
               brk = as.factor(c("<10", "10-20", "10-20", "10-20", "NA", "10-20", 
                                 "10-20", "10-20", "20-35", "<10", "10-20", "<10", 
                                 "35", "20-35", "<10", "NA", "10-20", "<10"))) |>
  ## mutate(color = recode(brk,
  ##                       "<10"   = "red",
  ##                       "10-20" = "green",
  ##                       "NA"    = "gray",
  ##                       "20-35" = "blue",
  ##                       "35"    = "black"))
  mutate(color = brewer.pal(6, "Set1")[brk])


set.seed(123)
sim.tree<-pbtree(n=18)
cols <- with(df, color[match(sim.tree$tip.label, tip)])


plot(sim.tree, tip.color = cols, x.lim = c(0,3))


with(df,
     legend(x=2.5, y=10,
            legend = levels(brk),
            col = brewer.pal(6, "Set1"),
            ## col = as.character(levels(color)),
            pch = 20,
            box.lwd = 1))

enter image description here

Upvotes: 1

Related Questions