[英]How can I plot colored tip labels in ggtree without including it as part of the legend?
我一直在嘗試使用ggtree
中的 ggtree package plot 一棵帶有顏色編碼的分支和提示的樹。 這是一個使用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","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))
我遇到的問題是生成的 plot 包含一個文本元素(一個小的“a”)作為圖例的一部分。 我一直無法弄清楚如何從圖例中省略這個文本元素。 我想保留圖例本身,但我不希望在上例中與紅線和藍線一起繪制的紅色和藍色“a”。
通常它就像不將顏色參數設置為元素標簽 (geom_tiplab) 中的 aes 一樣簡單。 但是,如果我不調用 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))
我收到一條錯誤消息,提示找不到 object“組”。 所以它似乎不像普通的ggplot那么簡單。
您可以通過在該 geom 調用中設置 boolean show.legend
從任何幾何圖形(至少,我認為是任何幾何圖形)中刪除向圖例添加的美學。 所以, 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,1),
legend.title=element_text(size=7),legend.text=element_text(size=7))
或者,您可以通過覆蓋圖例的美學來執行此操作而不使用show.legend
。 在這種情況下,您可能希望將所有標簽轉換為空字符串,因此圖例上顯示的 label 只是""
。 您可以通過guides()
做到這一點:
# add the following to your plot code
guides(color=guide_legend(override.aes = list(label="")))
您可以使用以下代碼在scale_color_manual()
中調整圖例 var label 的文本: 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.