YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
821 stars 172 forks source link

facet_plot will not plot correctly align to branch in ggtree #604

Open cms72 opened 5 months ago

cms72 commented 5 months ago

Hi - I have a set of descriptions (descriptons1) that I am trying to align to the tree. To separate these descriptions from another set of data, I used facet_plot. However, descriptions1 did not align to the branch - there were only two branches that contained description1

However, I did find that when there were > 5 branches with descriptions (see descriptions2), facet_plot worked.

Tree: ggtree.newick.txt


######## Descriptions 1 and 2 ########
description1 <- data.frame (ID  = c("978", "978", "978","978", "978","974","974","974","974","974","974","974","974"),
                            Type = c("value_A", "value_B", "value_C","value_D", "value_E", "value_F","value_G", "value_H", "value_I","value_J", "value_K", "value_L","value_M"),
                            Desc = c("Blue","Pink","Red","White","Green","Purple","Yellow","Orange","Brown","Blue","Green","Green","Green"),
                            xindex= c(1,2,3,4,5,6,7,8,9,10,11,12,13)
)

description2 <- data.frame (ID  = c("978", "978", "978","978", "978","974","974","974","974","974","974","974","974","374", "374", "374","374", "374","372", "372", "372","372", "372","3", "3", "3","3", "3"),
                            Type = c("value_A", "value_B", "value_C","value_D", "value_E", "value_F","value_G", "value_H", "value_I","value_J", "value_K", "value_L","value_M","value_A", "value_B", "value_C","value_D", "value_E","value_A", "value_B", "value_C","value_D", "value_E","value_A", "value_B", "value_C","value_D", "value_E"),
                            Desc = c("Blue","Pink","Red","White","Green","Purple","Yellow","Orange","Brown","Blue","Green","Green","Green","Blue","Pink","Red","White","Green","Blue","Pink","Red","White","Green","Blue","Pink","Red","White","Green"),
                            xindex= c(1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
)

######## Tree ########
ggtree<-read.tree("ggtree.newick.txt")
midpoint_ggtree<-midpoint.root(ggtree)
tree1 <- ggtree(midpoint_ggtree) + geom_tiplab(size=2)

tree2 <- facet_plot(tree1, panel = "Description1", data = description1, geom=geom_tile, mapping = aes(x=xindex, y=y, fill = Desc),size=1)+
  scale_fill_brewer(palette="Set1", na.value="transparent", direction=-1,name="Description1")

tree3 <- tree2 + new_scale_fill()

tree3 <- facet_plot(tree2, panel = "Description2", data = description2, geom=geom_tile, mapping = aes(x=xindex, y=y, fill = Desc),size=1)+
  scale_fill_brewer(palette="Set1", na.value="transparent", direction=-1,name="Description2")

tree3