Adding shapes from plotly R library to the phylogenetic tree tips generated by ggtree
0
0
Entering edit mode
8 months ago
Hann ▴ 110

Hello,

I have generated a figure of phylogenetic tree. This is my R script to generate phylogenetic tree. However, I would like to add different shapes as a lable (in the tree tip) not only one shape as it's now.

These shapes are from ploly R library as ggplot shapes are not enough for my dataset. I could not use shapes in ggplot2 as I need a lot of shapes so best options is plotly (I have generated PCA and other plots with the same shapes, so it's also to be consistent). See below file has the shapes.

# Load necessary libraries
library("ape")
library("ggtree")
library("plotly")
library(data.table)

# Replace these with your file paths
tree<-read.newick("minCov8_minmeanDP0_maxmissing0.8_remove_contaminatedIndvs_removeDupindvs_ldpruned_maf0.05_removePRZW221kb.boot_reroot.nwk")

#pdf("tree_test2.pdf", width = 15, height =80, pointsize = 1/300)
p <- ggtree(tree,layout="rectangular",size=0.1,ladderize=T)# +geom_tiplab(aes(label=label),size=2)
p
##get the tip labels to give colours, ordered as the tree
ladder<- ladderize(tree, right = FALSE)
is_tip <- ladder$edge[,2] <= length(ladder$tip.label)
ordered_tips <- ladder$edge[is_tip, 2]
names<-data.frame(ladder$tip.label[ordered_tips])
#make the colours for each tip
ETH5B<-rep("black", 1)
PRZW16<-rep("black", 1)
PRZW17<-rep("black", 1)
PRZW15<-rep("black", 1)
PRZW14<-rep("black", 1)
PRZW11<-rep("black", 1)
PRZW29<-rep("black", 1)
PRZW19<-rep("black", 1)
PRZW18<-rep("black", 1)
PRZW20<-rep("black", 1)
PRZW21<-rep("black", 1)
PRZW12<-rep("black", 1)
PRZW10<-rep("black", 1)
PRZW23<-rep("black", 1)
PRZW22<-rep("black", 1)
PRZW2<-rep("black", 1)
PRZW30<-rep("black", 1)
PRZW31<-rep("black", 1)
PRZW13<-rep("black", 1)
PRZW4<-rep("black", 1)
PRZW9<-rep("black", 1)
PRZW5<-rep("black", 1)
PRZW8<-rep("black", 1)
PRZW3<-rep("black", 1)
PRZW7<-rep("black", 1)
PRZW6<-rep("black", 1)
PRZW1<-rep("black", 1)
MONG8<-rep("palevioletred3", 1)
TSHU2<-rep("lightpink1", 1)
TSHU1<-rep("lightpink1", 1)
NOMA2<-rep("lightpink1", 1)
NOMA1<-rep("lightpink1", 1)
KISO2<-rep("lightpink1", 1)
KISO1<-rep("lightpink1", 1)
HOKK2<-rep("lightpink1", 1)
HOKK1<-rep("lightpink1", 1)
MISA2<-rep("lightpink1", 1)
MISA1<-rep("lightpink1", 1)
TOKA2<-rep("lightpink1", 1)
TOKA1<-rep("lightpink1", 1)
YONA2<-rep("lightpink1", 1)
YONA1<-rep("lightpink1", 1)
MIYA2<-rep("lightpink1", 1)
MIYA1<-rep("lightpink1", 1)
...etc

#merge together
colours<-c(
  ETH5B,PRZW16,PRZW17,PRZW15,PRZW14,PRZW11,PRZW29,PRZW19,PRZW18,PRZW20,PRZW21,PRZW12,PRZW10,PRZW23,PRZW22,PRZW2,PRZW30,PRZW31,PRZW13,PRZW4,PRZW9,PRZW5,PRZW8,PRZW3,PRZW7,PRZW6,PRZW1,
  MONG8,TSHU2,TSHU1,NOMA2,NOMA1,KISO2,KISO1,HOKK2,HOKK1,MISA2,MISA1,TOKA2,TOKA1,YONA2,YONA1,MIYA2,MIYA1 ...etc
)
tips<-data.frame(names,colours)
tips$family<-sub("\\_.*", "", tips$ladder.tip.label.ordered_tips.)
str(tips)
##add colours and tips into tree
tip.tree<-p %<+% tips +  geom_tippoint(aes(colour=colours), shape=1, size=0.8) +  # .... I need help here. I do not want one shape. I have the sapes in a table (see below) and I would like to use the same shapes for each tip name in the tree ... Shapes are from plotly R package. So I need to find a way to add plotly shapes to ggplot2 
  scale_colour_manual(breaks =tips$colours,values=tips$colours) +
  theme(legend.position="none")
tip.tree
#add in bootstraps
q <- ggtree(tree)
d <- q$data
d <- d[!d$isTip,]
d$label <- as.numeric(d$label)
d <- d[d$label > 90,]
d
tree<-tip.tree + geom_point(data=d,aes(node=node),shape=16, size=.4, fill='black',stat="identity") +
  theme(plot.margin = unit(c(0.15,0,0.8,0), "cm")) + 
  geom_treescale(x=0, y=350,width=0.02, color='black',offset=5) +  
  xlim(0, 0.3) 
tree
tree+ geom_text(aes(label=node), hjust=-.3)


## I found that to add options to the plot, first convert ggplot2 plot to Plotly variable with ggplotly() and then, add options to that variable.

fig <- ggplotly(tree) fig <- fig %>%    # ..... But I do not think in this case is possible

data frame with the shapes:

shapes <-fread("names_shapes.txt")

names   shapes ETH5B    circle PRZW16   circle-open PRZW17  circle-open
PRZW15  circle-open PRZW14  circle-open PRZW11  circle-open
PRZW29  circle-open PRZW19  circle-open PRZW18  circle-open
PRZW20  circle-open PRZW21  circle-open PRZW12  circle-open
PRZW10  circle-open PRZW23  circle-open PRZW22  circle-open
PRZW2   circle-open PRZW30  circle-open PRZW31  circle-open
PRZW13  circle-open PRZW4   circle-open PRZW9   circle-open
PRZW5   circle-open PRZW8   circle-open PRZW3   circle-open
PRZW7   circle-open PRZW6   circle-open PRZW1   circle-open MONG8   x-open
TSHU2   diamond-tall TSHU1  diamond-tall NOMA2  x NOMA1 x
KISO2   diamond-cross-open KISO1    diamond-cross-open
HOKK2   triangle-up-open HOKK1  triangle-up-open MISA2  diamond-wide
MISA1   diamond-wide TOKA2  cross-open TOKA1    cross-open
YONA2   star-diamond YONA1  star-diamond MIYA2  cross MIYA1 cross 
..
..

My current phylogenetic tree look like this (one shape on the tips). I would greatly appreciate your input and help. Thanks!

enter image description here

R phylogenetic-tree ggtree plotly • 332 views
ADD COMMENT

Login before adding your answer.

Traffic: 2185 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6