1
votes

I have been trying to plot a tree with color-coded branches and tips using the ggtree package in R. Here is an example code using a tree of Anolis lizards.

library(ggtree);library(tidyverse);library(ape)
anole.tree<-read.tree("http://www.phytools.org/eqg2015/data/anole.tre")
svl <- read.csv("http://www.phytools.org/eqg2015/data/svl.csv",row.names=1)
cls<-list(clade1=c("baleatus","barahonae","ricordii","eugenegrahami","christophei","cuvieri"),
          clade2=subset(anole.tree$tip.label,!(anole.tree$tip.label %in% c("baleatus","barahonae","ricordii","eugenegrahami","christophei","cuveri"))))
anole.tree_new<-groupOTU(anole.tree,.node=cls)
ggtree(anole.tree_new,layout="circular",ladderize=TRUE)+
  geom_tree(aes(color=group))+
  scale_color_manual(values=c("blue","red"))+
  geom_tiplab(size=0.8,aes(color=group))+
  theme(legend.position = c(0.9, 1),
        legend.justification = c(0,1),
    legend.title=element_text(size=7),legend.text=element_text(size=7))

The issue I am having with this is that the resulting plot includes a text element (a small "a") as part of the legend. I have been unable to figure out how to omit this text element from the legend. I want to keep the legend itself but I don't want the red and blue "a" that are plotted alongside the red and blue lines in the above example.

Normally it would be as simple as not setting the color argument to be an aes within the element labels (geom_tiplab). However, if I do not call group color under aes...

ggtree(anole.tree_new,layout="circular",ladderize=TRUE)+
  geom_tree(aes(color=group))+
  scale_color_manual(values=c("blue","red"))+
  geom_tiplab(size=0.8,color=group)+
  theme(legend.position = c(0.9, 1),
        legend.justification = c(0,1),
        legend.title=element_text(size=7),legend.text=element_text(size=7))

I get an error saying object "group" not found. So it doesn't seem to be as straightforward as in a normal ggplot.

2
using guide = FALSE option in scale_color_manual will remove it from legend.Sinh Nguyen
@SinhNguyen This just removes the entire legend. What I want is to keep the legend but remove the text from the legend symbol. I.e., in the example plotted, I want to remove the small red and blue "a" that is plotted alongside the blue and red line but keep those lines.user2352714

2 Answers

2
votes

You can remove the addition of the aesthetics to the legend from any geom (at least, I think any geom) by setting the boolean show.legend within that geom call. So, show.legend = FALSE seems to do the trick for me:

ggtree(anole.tree_new,layout="circular",ladderize=TRUE)+
  geom_tree(aes(color=group))+
  scale_color_manual(values=c("blue","red"))+
  geom_tiplab(size=0.8,aes(color=group), show.legend=FALSE)+
  theme(legend.position = c(0.9, 1),
        legend.justification = c(0,1),
        legend.title=element_text(size=7),legend.text=element_text(size=7))

enter image description here

Alternatively, you can do this without using show.legend if you want by overriding the aesthetics of the legend. In this case, you would want to turn all the labels into an empty string, so the label shown on the legend is just "". You can do this through guides():

# add the following to your plot code
guides(color=guide_legend(override.aes = list(label="")))
0
votes

You can tweak the text of the legend var label in scale_color_manual() with this code: label=c("whatever you want","more what ever")

ggtree(anole.tree_new,layout="circular",ladderize=TRUE)+
  geom_tree(aes(color=group))+
  scale_color_manual(values=c("blue","red"), label=c("whatever you want","more what ever"))+
  geom_tiplab(size=0.8,aes(color=group))+
  theme(legend.position = c(0.9, 1),
        legend.justification = c(0,1),
        legend.title=element_text(size=22),legend.text=element_text(size=22))

enter image description here