---
title: "Vision loss evo"
output: html_document
---
knitr::opts_chunk$set(
echo = FALSE, fig.path = './Analysis/plot-', cache = TRUE
)
##Load up packages, data and tree
```{r }
setwd("./Data/")
require(phytools)
require(ggtree)
require(dispRity)
require(RRphylo)
tree <-read.tree ("tree.nwk")
data <-read.csv ("acuityblind.csv",row.names=1)
clean.data(data,tree)
dat <- as.data.frame(data$VA)
row.names(dat)<-row.names(data)
dat$weight <- log(data$X5.1_AdultBodyMass_g)
names(dat) <- c("VA", "W")
dat$relVA <- c(dat$VA/dat$W)
names(dat) <- data$Bin
names(dat) <- c("VA", "W", "relVA")
```
##ANC
```{r , include=FALSE}
tree$edge.length[which(tree$edge.length == 0)] <- 0.01*median(tree$edge.length[-which(tree$edge.length == 0)])
tree <- force.ultrametric(tree,method="extend")
anc <- fastAnc(tree,data$VA, vars=TRUE,CI=TRUE, REML = 1)
#anc1<-fastAnc(tree,dat$relVA,vars=TRUE,CI=TRUE, REML = 1, na.omit = TRUE)
plot(tree,no.margin=TRUE,edge.width=2,cex=0.7)
nodelabels(text=anc$VA,frame="none",adj=c(1.1,-0.4), cex=0.5)
```
##Plotting
```{r, include=TRUE}
#pdf(file="treeblind.pdf",width=10,height=10)
fancyTree(tree,type="scattergram",X=as.matrix(dat),A=as.matrix(anc),control=list(spin=FALSE),label="horizontal")
#dev.off()
#OR
dat1 <- data$VA
names(dat1) <- row.names(dat)
#pdf(file="treeblind1.pdf",width=10,height=10)
obj1<-contMap(tree,dat1,fsize=c(0.6,1),outline=FALSE)
#plot(obj1,direction="leftwards")
#dev.off()
plot(obj1,type="fan",legend=0.7*max(nodeHeights(tree)), fsize=c(0.7,0.9))
phenogram(tree,dat1,fsize=0.6,spread.labels=TRUE,spread.cost=c(1,0))
fancyTree(tree,type="phenogram95",x=dat1,spread.cost=c(1,0))
#not working
obj<-contMap(tree,exp(dat1),plot=FALSE)
plotTree.wBars(obj$tree,dat1,method="plotSimmap",
tip.labels=TRUE,fsize=0.7,colors=obj$cols,type="fan",scale=0.002)
add.color.bar(1.0,obj$cols,title="trait value",lims=obj$lims,prompt=FALSE,
x=0.9*par()$usr[1],y=0.9*par()$usr[3])
```
##Plotting 2
```{r, include=TRUE}
#resetting the plot space
#old.par <- par(mar = c(0, 0, 0, 0))
#par(old.par)
pdf(file="treeblind2.pdf",width=10,height=10)
plot(tree,label.offset=5, cex= 0.7)
nodelabels(cex=anc$ace/30,pch=16)
tiplabels(cex=dat$VA/70,pch=16)
dev.off()
```
RRphylo
```{r}
RR <- RRphylo(tree, dat1)
#search.conv(RR,tree,dat1, PGLSf=TRUE,nsim=1000,rsim=1000, clus=.5, min.dist = 3, foldername ="Data")
search.shift(RR, auto.recognize = "yes", test.single = "yes", status.type= "clade", nrep = 10000, f=2, foldername ="Data")
#cov = can add covariate
#### Circular tree plot
np <- ggtree(tree, layout="circular") +
geom_hilight(node=176, fill="firebrick2", alpha=.6) +
geom_hilight(node=224, fill="limegreen", alpha=.6) +
geom_hilight(node=198, fill="firebrick2", alpha=.6)
np + geom_cladelabel(176, 'Chiroptera', angle=315, hjust = "center", offset.text=13, barsize=3.5, color='red4', offset = 2.5, fontsize = 5) +
geom_cladelabel(224, 'Haplorhini', angle=70, hjust = "center", offset.text=13, barsize=3.5, color = 'darkgreen', offset = 2.5, fontsize = 5) +
geom_cladelabel(198, 'Rodentia', angle=355, hjust = "center", offset.text=13, barsize=3.5, color='red4', offset = 2.5, fontsize = 5) +
geom_cladelabel(238, 'Afrotheria', angle=315, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(123, 'Marsupialia', angle=295, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(158, 'Artiodactyla', angle=60, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(143, 'Carnivora', angle=10, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(157, 'Perissodactyla', angle=345, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(138, 'Eulipotyphla', angle=280, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(219, 'Strepsirrhini', angle=40, hjust = "center", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 3) +
geom_cladelabel(237, 'Scandentia', angle=26, hjust = "left", offset.text=13, barsize=1, color='black', offset = 2.5, fontsize = 2.5)
```