YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
821 stars 172 forks source link

Tree tips not colored according to specified colors #602

Open JakobDrumm opened 5 months ago

JakobDrumm commented 5 months ago

I am generating a phylogenetic tree for 27 different fungi species, each species ieither has the trait "necrotrophic", "biotrophic" or "hemibiotrophic", i now want the species labels to be colored black for necrotrophic fungi, green for biotrophic fungi and gray for hemibiotrophic fungi. The used data contains trait, expected color, species name and taxon id. When running the code below the program simply returns an error, demanding a color from a hue palette.

load libraries

library(tidyverse) library(dplyr) library(phylostratr) library(ggtree) library(ggimage) library(TDbook)

import list of species

Taxon_List=read.csv(file="Data/Fungi_Species_CSV.csv", sep=";", header=FALSE)

data cleanup

Taxon_List=Taxon_List %>% rename( Species= V1, Division= V2, Trait= V3, NCBI_Taxon_ID = V4, ) Taxon_List_clean=Taxon_List[((Taxon_List$Trait=="biotroph") | (Taxon_List$Trait=="necrotroph") | (Taxon_List$Trait=="hemibiotroph") ),] Taxon_List_clean=mutate(Taxon_List_clean, cols = recode(Trait, "biotroph"="green", "necrotroph"="black", "hemibiotroph"="gray" ) )

generate tree

vector=c(Taxon_List_clean$NCBI_Taxon_ID) fungi_tree=ncbi_tree(taxa=vector)

plotting

p = ggtree(fungi_tree) p= p %<+% Taxon_List_clean + geom_tiplab(aes(x, y, color=cols)) + geom_highlight(node=28, fill="purple", type="rect", to.bottom=FALSE) + geom_highlight(node=31, fill="black", type="rect", to.bottom=FALSE) + geom_highlight(node=44, fill="blue", type="rect", to.bottom=FALSE) + ggtitle(label="Observed fungi species phylogenetic tree", subtitle="biotrophic in green, necrotrophic in black, hemibiotrophic in gray" ) plot(p)

expected: Tree where the NCBI Taxon tip labels are colored green for biotrophic, black for necrotrophic and gray for hemibiotrophic fungi

results: error message when plotting: Error in palette():

! Must request at least one colour from a hue palette.

brj1 commented 5 months ago

I cannot reproduce the error message. I don't know what your tree (which package is ncbi_tree from?) or CSV file look like.

Though this will probably won't fix your issue, assigning colour to plot elements does not work in ggplot by passing the colours directly through an aesthetic. You want to put the parameter to be coloured (in your case Trait) in the aes call and then modify the colouring by scaling the aesthetic (adding scale_colour_manual). See below:

p = ggtree(fungi_tree)
p= p %<+% Taxon_List_clean +
geom_tiplab(aes(x, y, color=Trait)) +
geom_highlight(node=28, fill="purple", type="rect", to.bottom=FALSE) +
geom_highlight(node=31, fill="black", type="rect", to.bottom=FALSE) +
geom_highlight(node=44, fill="blue", type="rect", to.bottom=FALSE) +
ggtitle(label="Observed fungi species phylogenetic tree",
subtitle="biotrophic in green, necrotrophic in black, hemibiotrophic in gray"
) + 
scale_colour_manual(values = c("biotroph"="green",
"necrotroph"="black",
"hemibiotroph"="gray"
))
floridaman-93 commented 4 months ago

Hi @JakobDrumm @brj1 I have experienced a similar issue. The correction proposed by @brj1 doesn't work. I had a script ready autumn last year and was generating this plot without any issues. This is something new. @brj1 The code will go through after applying your correction, but the tips remain gray. The plot legend says that under grouping variable, you have "NA". Anyone else has an idea how to solve this?