I have a phylogenetic tree:
my.tree <- ape::read.tree(text = "((rat:0.06290316531,mouse:0.06094803666):0.05175420892,human:0.09883650566);")
which I then convert to a ggdend object:
library(dplyr)
my.dend <- phylogram::as.dendrogram.phylo(my.tree) %>%
dendextend::hang.dendrogram() %>%
dendextend::hang.dendrogram(hang = -1) %>%
dendextend::as.ggdend()
I would like to plot my.dend using ggplot2 and to add to the leaves pie charts.
Here is the data.frame that describes the pie for each leaf:
set.seed(1)
labels.df <- data.frame(label = c("human","mouse","rat"),t(apply(matrix(runif(9,0,1),3,3),1,function(x) x/sum(x))),check.names = F) %>%
dplyr::left_join(my.dend$labels %>% dplyr::select(x,y,label))
I thought that combining the plotting of my.dend with labels.df using scatterpie's geom_scatterpie can work.
So I do:
library(ggplot2)
library(scatterpie)
ggplot(my.dend,labels=F,horiz=T)+guides(fill=F)+coord_flip()+annotate("text",size=4.5,hjust=0,x=my.dend$label$x,y=my.dend$label$y,label=my.dend$label$label)+labs(x="",y="")+theme_minimal()+
theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank(),legend.position="none",legend.text=element_blank(),legend.background=element_blank(),legend.key=element_blank())+
geom_scatterpie(aes(x=x,y=y+0.05),data=labels.df,color=NA,cols=as.character(1:3))
Which gives:
If add +coord_flip() to the end:
ggplot(my.dend,labels=F,horiz=T)+guides(fill=F)+coord_flip()+annotate("text",size=4.5,hjust=0,x=my.dend$label$x,y=my.dend$label$y,label=my.dend$label$label)+labs(x="",y="")+theme_minimal()+
theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank(),legend.position="none",legend.text=element_blank(),legend.background=element_blank(),legend.key=element_blank())+
geom_scatterpie(aes(x=x,y=y+0.05),data=labels.df,color=NA,cols=as.character(1:3))+coord_equal()
The pies do not get distorted but the tree flips to become vertical - root facing down:

Any idea how to get the pies not distorted and the tree horizontal with the root on the left?

Looks like it'll be easier to plot the phylogeny and
scatterpieseparately and then combine them:Data:
Plots:
And we get:
Would be nice to be able to shrink some of the space that separates the tree from the pies, if anyone can add that.