# ggtree：How to keep leading zero ‘0’ in the tiplabel？

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)


Some taxa in the figure

## 1 Answer

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.

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