Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree tips not colored according to specified colors #602

Open
JakobDrumm opened this issue Feb 7, 2024 · 2 comments
Open

Tree tips not colored according to specified colors #602

JakobDrumm opened this issue Feb 7, 2024 · 2 comments

Comments

@JakobDrumm
Copy link

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
Copy link
Contributor

brj1 commented Feb 8, 2024

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants