我正在尝试绘制系统发育树(以dendextend::ggdend
对象的形式)以及scatterpie
在树的内部节点中表示祖先状态。
这是系统发育物种树:
species.tree <- ape::read.tree(text="(Platypus:0.1286194212,((((Koala:0.05906053125,Wallaby:0.06286946875):0.01269935535,Tasmanaian_devil:0.06666064465):0.002343400539,Opposum:0.07530014768):0.04236089288,((Armadillo:0.07766621875,Sloth:0.07828378125):0.02796723647,(((Rabbit:0.1031544407,(((Guinea_pig:0.07446361576,Chinchilla:0.07034397799):0.005464430664,(Naked_mole-rat:0.05677127221,Damarland_mole-rat:0.05767394654):0.01936294186):0.02759208881,(Jerboa:0.0947315755,(Blind_mole-rat:0.08812192941,((((Chinese_hamster:0.04926318023,Golden_hamster:0.05247866352):0.01638038784,Deer_mouse:0.06370916667):0.003237569002,Prairie_vole:0.07030201433):0.01060310787,(Rat:0.06290316531,Mouse:0.06094803666):0.01696014951):0.01220825015):0.009753667035):0.007422592018):0.004040214171):0.001369336039,(Mouse_lemur:0.07642478568,(Tarsier:0.07963153014,((Marmoset:0.03015044025,Mas_night_monkey:0.02517624473):0.02351744565,((((((Bonobo:0.00336025,Chimpanzee:0.00217975):0.004281320755,Human:0.006628679245):0.001853863546,Gorilla:0.008585696203):0.00845141498,Orangutan:0.01712893312):0.002774193206,Gibbon:0.01958316667):0.01096529033,(Green_monkey:0.01171472581,((Sooty_mangabey:0.006316729363,Baboon:0.006640801887):0.001116431826,(Macaque:0.003279600629,Pig-tailed_macaque:0.004228899371):0.004415690816):0.003992496416):0.01846812712):0.01778527361):0.02771818883):0.0005639189206):0.02209568299):0.0001,((European_hedgehog:0.1008965,Common_shrew:0.1078135):0.0009739982143,(Little_brown_bat:0.08947140351,((Cat:0.06468470992,(Dingo:0.00096371875,Dog:0.00134628125):0.06356096643):0.01565630777,((Dolphin:0.06816704487,(Pig:0.07646753755,((Goat:0.0113001875,Sheep:0.0110398125):0.01697194969,Cow:0.02692305031):0.05571845839):0.0001):0.01229828424,(Horse:0.00636975,Donkey:0.00667025):0.07594974567):0.004203855474):0.001715181365):0.01516209959):0.0001):0.0001):0.0145995455):0.006614758937);")
现在将其转换为dendextend::ggdend
对象:
library(ggplot2)
species.dend <- phylogram::as.dendrogram.phylo(species.tree) %>%
dendextend::hang.dendrogram() %>%
dendextend::hang.dendrogram(hang = -1) %>%
dendextend::as.ggdend()
绘图species.dend
:
ggplot(species.dend,labels=F,horiz=T)+guides(fill=F)+coord_flip()+annotate("text",size=4.5,hjust=0,x=species.dend$label$x,y=species.dend$label$y,label=species.dend$label$label)+labs(x="",y="")+theme_minimal()+theme_void()
现在我要添加祖先状态数据——data.frame
对于树中的每个内部节点 (state0
和state1
),它具有两个祖先状态,每个状态的概率在 0 和 1 之间:
species <- species.tree$tip.label
ancestral.state.df <- species.dend$nodes %>%
dplyr::filter(is.na(leaf)) %>%
dplyr::select(x,y,cex,members) %>%
dplyr::mutate(node.idx = members+length(species)-1) %>%
dplyr::select(x,y,node.idx) %>%
dplyr::left_join(data.frame(node.idx=as.numeric(c(95,94,51,50,49,90,49,88,75,60,59,51,49,49,55,54,53,51,50,49,49,62,61,60,49,58,53,52,51,50,49,52,51,49,49,60,49,58,57,50,49,54,52,51,50,49,49)),
state0=c(0,0,1,1,1,0.5,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1),
state1=c(1,1,0,0,0,0.5,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0),stringsAsFactors = F),by=c("node.idx"="node.idx")) %>%
unique()
现在我想绘制species.dend
映射到它们的祖先节点xy
坐标的祖先状态,如ancestral.state.df
. 我认为这应该工作:
library(scatterpie)
ggplot(species.dend,labels=F,horiz=T)+guides(fill=F)+coord_flip()+annotate("text",size=4.5,hjust=0,x=species.dend$label$x,y=species.dend$label$y,label=species.dend$label$label)+labs(x="",y="")+theme_minimal()+theme_void()+
geom_scatterpie(aes(x=y,y=x,r=0.3),data=ancestral.state.df,cols=c("state0","state1"))+
coord_equal()+labs(x="",y="",fill="Ancestral State")+
theme_minimal()+theme_void()
知道如何将祖先状态作为饼图获取,位于祖先节点的 xy 坐标处species.dend
?