2
$\begingroup$

I am using ggtree to draw a phylogenetic tree.

Some taxa IDs have a 0 at the beginning.

However, when I plot the tiplabel the leading zero is automatically dropped. How do I retain the leading zero?

Thanks.

The code and tree file and metadata file are attached. I would appreciate any help.

setwd('G:\\Experiments\\Paper\\Torula\\06_iqtree')   
dir()

# Load required packages
library(xlsx)
library(ggtree)
library(treeio)
library(ggplot2)

# Read tree
tre <- read.iqtree("concatenate_iqtree2.treefile")
tre_tbl <- fortify(tre)
# Read associated information table
metadf <- read.xlsx('G:\\Experiments\\Paper\\Torula\\00_data\\Torula_taxa.xlsx', sheetIndex = 1)

# Plot tree
ggtree(tre) %<+% metadf + 
    # our own taxa darkblue
    # type strain bold
    geom_tiplab(aes(subset = (New == 1 & Type == 1), 
                    label = paste0('bolditalic(', Genus, ')~bolditalic(', Epithet, ')~bold(', Collection,')~bold(', Number,')')), 
                color = 'darkblue', parse = T) + 
    geom_tiplab(aes(subset = (New == 0 & Type == 1), 
                    label = paste0('bolditalic(', Genus, ')~bolditalic(', Epithet, ')~bold(', Collection,')~bold(', Number,')')), 
                color = 'black', parse = T) + 
    geom_tiplab(aes(subset = (New == 0 & Type == 0), 
                    label = paste0('italic(', Genus, ')~italic(', Epithet, ')', '~', Collection,'~', Number)), 
                color = 'black', parse = T) + 
    
    geom_text(aes(label= UFboot), vjust = 0, hjust = 1, size = 3) +
    xlim(NA,0.5)

enter image description here Some taxa in the figure enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

I like both the methods and the study. ggplot2 is R's standout package/library and the library ggtree looks great.

Anyway, I would know how to solve this in Python or Perl and I'll use the same logic for R. My suspicion is R is automatically type casting your metadata and therefore has no option but to remove the zero.

Simply convert your integer range into a string, viz.

 Number2 = as.character(Number)

or simply in situ

as.character(Number)

This doesn't make any difference to your output, who cares whether its and integer or string and forces R into retaining the zero, '0'.


Note below that tried and didn't work. A string should be immutable. The suspicion is the the '0' is being removed at an earlier point in the code, i.e. as soon as it type casts it - zero disappears.

What I would do some print outs of the variable between its import and writing to disk. What I suspect is the conversion to string needs to occur earlier.


Second idea The other possibility is to wrap the metadata in double/single quotes in the Excel file (or flat file). This way R cannot force it into an integer when it is imported and must retain it as a string. I'd be amazed if R could wriggle out of that.

$\endgroup$
4
  • $\begingroup$ I already tried that, but it did not work. Thanks anyway. $\endgroup$
    – YP CHEN
    May 20, 2022 at 14:16
  • $\begingroup$ I've addressed this above @YPCHEN. I'm pretty certain this is a type cast bug and no more. $\endgroup$
    – M__
    May 20, 2022 at 15:48
  • $\begingroup$ I quote the metadata in the excel, it works. Thank you very much. $\endgroup$
    – YP CHEN
    May 21, 2022 at 2:38
  • $\begingroup$ Glad it works @YPCHEN please do remember to 'accept' the answer. $\endgroup$
    – M__
    May 21, 2022 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.