<p>I like both the methods and the study. <code>ggplot2</code> is R's standout package/library and the library <code>ggtree</code> looks great.</p>
<p>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.</p>
<p>Simply convert your integer range into a string, viz.</p>
<pre><code> Number2 = as.character(Number)
</code></pre>
<p>or simply <em>in situ</em></p>
<pre><code>as.character(Number)
</code></pre>
<p>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'.</p>
<hr />
<p>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.</p>
<p>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.</p>
<hr />
<p><strong>Second idea</strong>
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.</p>

CC BY-SA 4.0
2022-05-20T14:16:29.753Z
I already tried that, but it did not work. Thanks anyway.
YP CHEN
CC BY-SA 4.0
2022-05-20T15:48:42.450Z
I've addressed this above @YPCHEN. I'm pretty certain this is a type cast bug and no more.
M__
CC BY-SA 4.0
2022-05-21T02:38:16.523Z
I quote the metadata in the excel, it works. Thank you very much.
YP CHEN
CC BY-SA 4.0
2022-05-21T02:42:55.033Z
Glad it works @YPCHEN please do remember to 'accept' the answer.
M__
CC BY-SA 4.0
2022-05-19T12:07:48.680Z
2022-05-20T15:48:08.410Z
2022-05-20T15:48:08.410Z
M__
<p>I am using <code>ggtree</code> to draw a phylogenetic tree.</p>
<p>Some taxa IDs have a 0 at the beginning.</p>
<p>However, when I plot the tiplabel the leading zero is automatically dropped. How do I retain the leading zero?</p>
<p>Thanks.</p>
<p>The code and tree file and metadata file are attached. I would appreciate any help.</p>
<pre><code>setwd('G:\\Experiments\\Paper\\Torula\\06_iqtree')   
dir()

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

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

# Plot tree
ggtree(tre) %&lt;+% metadf + 
    # our own taxa darkblue
    # type strain bold
    geom_tiplab(aes(subset = (New == 1 &amp; Type == 1), 
                    label = paste0('bolditalic(', Genus, ')~bolditalic(', Epithet, ')~bold(', Collection,')~bold(', Number,')')), 
                color = 'darkblue', parse = T) + 
    geom_tiplab(aes(subset = (New == 0 &amp; Type == 1), 
                    label = paste0('bolditalic(', Genus, ')~bolditalic(', Epithet, ')~bold(', Collection,')~bold(', Number,')')), 
                color = 'black', parse = T) + 
    geom_tiplab(aes(subset = (New == 0 &amp; 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)
</code></pre>
<p><a href="https://i.sstatic.net/Yafxt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Yafxt.png" alt="enter image description here" /></a>
Some taxa in the figure <a href="https://i.sstatic.net/D0K42.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/D0K42.png" alt="enter image description here" /></a></p>

<phylogenetics><phylogeny><ggplot2><ggtree>
ggtree：How to keep leading zero ‘0’ in the tiplabel？
# ggtree：How to keep leading zero ‘0’ in the tiplabel？

(Asked by: username_0 on 2022-05-19)

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

**Tags:** phylogenetics, phylogeny, ggplot2, ggtree

---

## 1 answers

### Answer by username_1 on 2022-05-19. Score: 2 | Accepted 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.

> **Comments:**
> **username_0**: I already tried that, but it did not work. Thanks anyway.  
> **username_1**: I've addressed this above @YPCHEN. I'm pretty certain this is a type cast bug and no more.  
> **username_0**: I quote the metadata in the excel, it works. Thank you very much.  
> **username_1**: Glad it works @YPCHEN please do remember to 'accept' the answer.  

---

