如何在 ggtree 中绘制彩色提示标签而不将其作为图例的一部分?

问题描述

我一直在尝试使用 R 中的 ggtree 包绘制带有颜色编码分支和尖端的树。以下是使用 Anolis 蜥蜴树的示例代码。>

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","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,legend.title=element_text(size=7),legend.text=element_text(size=7))

我遇到的问题是生成的图包含一个文本元素(一个小的“a”)作为图例的一部分。我一直无法弄清楚如何从图例中省略这个文本元素。我想保留图例本身,但我不想要在上面的示例中与红线和蓝线一起绘制的红色和蓝色“a”。

通常,它就像不在元素标签 (geom_tiplab) 中将颜色参数设置为 aes 一样简单。但是,如果我在 aes 下不调用组颜色...

ggtree(anole.tree_new,color=group)+
  theme(legend.position = c(0.9,legend.text=element_text(size=7))

我收到一条错误消息,说找不到对象“组”。所以它似乎不像普通的 ggplot 那样简单。

解决方法

您可以通过在该 geom 调用中设置布尔值 show.legend 从任何 geom(至少,我认为任何 geom)中删除对图例的美学添加。所以,show.legend = FALSE 似乎对我有用:

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,legend.title=element_text(size=7),legend.text=element_text(size=7))

enter image description here

或者,如果您愿意,您可以通过覆盖图例的美感来执行此操作,而无需使用 show.legend。在这种情况下,您可能希望将所有标签转换为空字符串,因此图例上显示的标签仅为 ""。您可以通过 guides():

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

您可以使用以下代码调整 scale_color_manual() 中图例变量标签的文本: label=c("whatever you want","more what ever")

ggtree(anole.tree_new,"red"),label=c("whatever you want","more what ever"))+
  geom_tiplab(size=0.8,aes(color=group))+
  theme(legend.position = c(0.9,legend.title=element_text(size=22),legend.text=element_text(size=22))

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...