rggplot2ggtreeggtext

how to apply both italic and normal fonts in the same label in phylogenetic tree in ggtree


I want A in italic, and CBS in normal. I think ggtext might be useful, but I got an error. here is an example:

tree<-read.tree(text="(A,(B,C));")
labs=c("*A*CBS","B","C")
tree$tip.label<-labs
ggtree(tree)+ geom_tiplab(align=T) + geom_richtext()

error: geom_rich_text requires the following missing aesthetics: label

I also tried

ggtree(tree)+ aes(label=labs)+geom_tiplab(align=T) + geom_richtext()
error: Aesthetics must be either length 1 or the same as the data (5): label

but the rich text I need is in three tip labels, not all five labels (tip and node) does anyone know how to add the label aesthetics (as tip label)?


Solution

  • You could use the parse = T argument combined with mathematical annotation:

    library(ggtree)
    library(ggtext)
    tree<-read.tree(text="(A,(B,C));")
    labs=c("paste(italic('A'),'CBS')","B","C")
    tree$tip.label<-labs
    ggtree(tree)+ geom_tiplab(align=T,parse=T)   
    

    enter image description here