Reputation: 356
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.
Upvotes: 2
Views: 2246
Reputation: 78937
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))
Upvotes: 0
Reputation: 13833
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))
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="")))
Upvotes: 3