---
title: "Phylomoji with ggtree and emojifont"
author: "\\

	Guangchuang Yu\\

        School of Public Health, The University of Hong Kong"
date: "`r Sys.Date()`"
bibliography: emojifont.bib
output:
  prettydoc::html_pretty:
    toc: true
    theme: cayman
    highlight: github
  pdf_document:
    toc: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{phylomoji}
  %\VignetteDepends{ggplot2}
  %\VignetteDepends{ggtree}
  %\VignetteDepends{colorspace}
  %\usepackage[utf8]{inputenc}
---

```{r style, echo=FALSE, results="asis", message=FALSE}
knitr::opts_chunk$set(tidy = FALSE,
		   message = FALSE)
```


```{r echo=FALSE, results="hide", message=FALSE}
library("ggplot2")
library("emojifont")
library("colorspace")
```


# Phylomoji

With ggtree [@yu_ggtree_2017], it is very easy to create phylomoji. Emoji is internally supported by ggtree.

## Use emoji in taxa labels

```{r fig.showtext=TRUE}
library(ggtree)
tree_text <- "(((((cow, (whale, dolphin)), (pig2, boar)), camel), fish), seedling);"
x <- read.tree(text=tree_text)
ggtree(x, linetype="dashed", color='firebrick') +
    xlim(NA, 7) + ylim(NA, 8.5) +
    geom_tiplab(aes(color=label), parse='emoji', size=14, vjust=0.25) +
    labs(title="phylomoji", caption="powered by ggtree + emojifont")

```


```{r fig.showtext=TRUE}
p <- ggtree(x, layout='circular') +
    geom_tiplab2(aes(color=label), parse='emoji', size=12, vjust=0.25)
print(p)
```

```{r fig.showtext=TRUE}
open_tree(p, angle=200)

open_tree(p, angle=60) %>% rotate_tree(-75)
```


## Labeling clades using emoji

```{r fig.showtext=TRUE}
set.seed(123)
tr <- rtree(30)
ggtree(tr) + xlim(NA, 5) +
    geom_cladelabel(node=41, label="chicken", parse="emoji",
                    fontsize=12, align=TRUE, color="firebrick") +
    geom_cladelabel(node=51, label="duck", parse="emoji",
                    fontsize=12, align=TRUE, color="steelblue") +
    geom_cladelabel(node=32, label="family", parse="emoji",
                    fontsize=12, align=TRUE, color="darkkhaki")
```


## Apple Color Emoji

Although `R`'s graphical devices don't support `AppleColorEmoji` font, it's still possible to use it. We can export the `plot` to `svg` file and render it in `Safari`.

```r
library(ggimage)
library(gridSVG)
p <-  ggtree(x, size=2) + geom_tiplab(size=20, parse='emoji') +
    xlim(NA, 7) + ylim(NA, 8.5) +
    geom_phylopic(image="79ad5f09-cf21-4c89-8e7d-0c82a00ce728",
                  color="firebrick", alpha = .3, size=Inf)

p
ps = grid.export("emoji.svg", addClass=T)
```

![](figures/apple_emoji2.png)


## Further reading

Please visit ggtree homepage, <https://guangchuangyu.github.io/ggtree>, to find out more.


# References

