rplotdata-visualizationphylogenyape-phylo

Is there a way to manually adjust the boundaries of a color gradient on a phylogeny in ape/phytools?


I am trying to visualize the results of a phylogenetic least squares regression using ape and phytools. Specifically, I have been trying to create a regression equation for predictive purposes, and I am looking at how much phylogenetic signal influences the residuals (and hence the accuracy of the equation). I have been using a code somewhat similar to the following to plot the results (albeit here retooled for dummy data).

library("ape")
library("phytools")
orig_tree<-rtree(n=20)
plot(orig_tree)
values<-data.frame("residuals"=runif(20,min=-1,max=1),row.names=orig_tree$tip.label)
values<-setNames(values$residuals,rownames(values))
residualsignalfit<-fastAnc(orig_tree,values,vars=TRUE,CI=TRUE)
obj<-contMap(orig_tree,values,plot=FALSE)
plot(obj,fsize=.25)

However, the problem comes in that I have a few species that exhibit extremely high residuals relative to the rest of the dataset. Because the minimum and maximum values of the color gradient are set to the minimum and maximum values of the actual column, this washes out all the contrast between 90% of the dataset to visualize the few extreme outlier values. Below is code that reproduces an example of what I mean compared to obj above.

values2<-values
values2[6]<--2
values2[7]<-2
residualsignalfit2<-fastAnc(orig_tree,values2,vars=TRUE,CI=TRUE)
obj2<-contMap(orig_tree,values2,plot=FALSE)
plot(obj2,fsize=.25)

This causes the figure to make it seem as though there is much less phylogenetic signal than there really is because it colors all but the most extreme outlier points to be similar in color.

I am trying to figure out a way to set the minimum and maximum of the color gradient so that any value ≤ -1 is the maximum possible red value and any value ≥ 1 is the maximum possible blue value, thereby allowing greater contrast in the rest of the residuals. I tried using the command

plot(obj2,fsize=.25,lims=c(-1,1))

but as you can see from this code this does nothing. I know ggplot2 has an option to rescale the color gradient based on user-inputted values, but I can't seem to figure out how to make phylogeny objects from ape or phytools get plotted in ggplot2.

Given this, is there any way to manipulate the color gradient pattern in ape/phytools such that one can arbitrarily set the maximum and minimum boundaries for the color gradient?


Solution

  • You could manipulate the color gradient pattern by "squeezing" the values between some arbitrary boundaries (in my example the 90% quantiles) to adjust the color gradient in phytools::contMap:

    ## The vector of values with two outliers
    values_outliers <- values
    values_outliers[6] <- -10
    values_outliers[7] <- 10
    
    ## The original heat plot object
    contMap_with_outliers <- contMap(orig_tree, values_outliers, plot = FALSE)
    
    plot(contMap_with_outliers, fsize = .25)
    
    ## Removing the outliers (setting them within to the 90% CI)
    values_no_outliers <- values_outliers
    ## Find the 90% boundaries
    boundaries <- quantile(values_no_outliers, probs = c(0.05, 0.95))
    ## Changing the values below the lowest boundary
    values_no_outliers <- ifelse(values_no_outliers < boundaries[1], boundaries[1], values_no_outliers)
    ## Changing the values above the highest boundary
    values_no_outliers <- ifelse(values_no_outliers > boundaries[2], boundaries[2], values_no_outliers)
    
    ## The heat plot object without outliers
    contMap_without_outliers <- contMap(orig_tree, values_no_outliers, plot = FALSE)
    
    plot(contMap_without_outliers, fsize = .25)