title: In R when coloring a phylogenetic tree by a trait using tip.color(), how do you choose colors and add the legend?
body: <p>I have a tree that looks like this;</p>
  <pre><code># Create tree
  library(phytools)
  sim.tree&lt;-pbtree(n=18)
  plot(sim.tree)
  </code></pre>
  <p>I have a data frame with my trait (a vector of class factor) to color like this;</p>
  <pre><code># data frame with vector to color tree with
  df&lt;-data.frame(tip = paste0(&quot;t&quot;, 1:18),
                 vector.to.color.with = as.factor(c(&quot;&lt;10&quot;, &quot;10-20&quot;, &quot;10-20&quot;, &quot;10-20&quot;, &quot;NA&quot;, &quot;10-20&quot;, 
                                     &quot;10-20&quot;, &quot;10-20&quot;, &quot;20-35&quot;, &quot;&lt;10&quot;, &quot;10-20&quot;, &quot;&lt;10&quot;, 
                                     &quot;35&quot;, &quot;20-35&quot;, &quot;&lt;10&quot;, &quot;NA&quot;, &quot;10-20&quot;, &quot;&lt;10&quot;)))
  </code></pre>
  <p>With some great assistance from SO I was able to color the tip labels like this;</p>
  <pre><code>cols &lt;- as.integer(df$vector.to.color.with[match(sim.tree$tip.label,df$tip)])
  plot(sim.tree, tip.color = cols)
  </code></pre>
  <p>I am now wanting to choose my own colors, plus add a legend. Any help is greatly appreciated.</p>