[英]Combining a ggdend tree with a geom_scatterpie
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: 然后我将其转换为ggdend
对象:
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. 我想使用ggplot2
绘制my.dend
并添加到叶子饼图中。
Here is the data.frame
that describes the pie for each leaf: 这是描述每个叶子的饼图的data.frame
:
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. 我认为的绘图结合my.dend
与labels.df
使用scatterpie
的geom_scatterpie
可以工作。
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: 如果在末尾添加+coord_flip()
:
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 scatterpie
separately and then combine them: 看起来分别绘制系统发育图和scatterpie
,然后将它们组合起来会更容易:
Data: 数据:
my.tree <- ape::read.tree(text = "((rat:0.06290316531,mouse:0.06094803666):0.05175420892,human:0.09883650566);")
library(dplyr)
my.dend <- phylogram::as.dendrogram.phylo(my.tree) %>%
dendextend::hang.dendrogram() %>%
dendextend::hang.dendrogram(hang = -1) %>%
dendextend::as.ggdend()
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))
library(ggplot2)
library(scatterpie)
Plots: 图:
my.dend <- 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_void()
my.scatterpie <- ggplot()+geom_scatterpie(aes(x=y,y=x,r=0.1),data=labels.df,color=NA,cols=as.character(1:3))+coord_equal()+labs(x="",y="",fill="Cluster")+theme_minimal()+theme_void()
gridExtra::grid.arrange(grobs=list(my.dend,my.scatterpie),ncol=2,nrow=1)
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. 如果有人可以添加的话,能够缩小将树与派分开的空间会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.