## Thursday, November 16, 2017

### Even 'hackier' solution to offsetting the tip labels in a plotted fan-style tree

In the not too distant past I posted a 'hack' to offset the tip labels of a fan-style tree plotted with phytools.

Unfortunately, this hack won't work for some functions that use `plotTree(...,type="fan")` internally, such as `plotTree.wBars`. This becomes important when one, say, wants to include (say) node labels on a plotted tree with bars.

Here's an even 'hackier' hack that does. I'm going to use a tree of Anolis lizards along with some real data for SVL (body size).

Here goes:

``````## first load libraries
library(phytools)
## this will be our tree
data(anoletree)
## I'm going to use this later
ecomorph<-as.factor(getStates(anoletree,"tips"))
## convert my "simmap" object to a regular old tree
tree<-as.phylo(anoletree)
## plus make sure the tips are in 'cladewise' order
## get our data
svl<-setNames(X[,"SVL"],rownames(X))
## fix the names
names(svl)<-paste("Anolis",names(svl),sep="_")
## sort into the order of the tip labels & back transform to original scale
## (for fun)
svl<-exp(svl[tree\$tip.label])
## now identify the tip labels on each side of our fan!
ii<-c(1:floor(0.25*Ntip(tree)),
ceiling(0.75*Ntip(tree)):Ntip(tree))
jj<-setdiff(1:Ntip(tree),ii)
hack<-tree
hack\$tip.label[ii]<-paste("  ",tree\$tip.label[ii],sep="")
hack\$tip.label[jj]<-paste(tree\$tip.label[jj],"  ",sep="")
SCALE<-0.02 ## for later
plotTree.wBars(hack,setNames(svl,hack\$tip.label),
type="fan",tip.label=T,fsize=0.6,scale=SCALE,col="blue",
width=0.5,lwd=1)
## now our labels!
mtrees<-make.simmap(tree,ecomorph,model="ER",nsim=100)
``````
``````## make.simmap is sampling character histories conditioned on the transition matrix
##
## Q =
##             CG          GB          TC          TG          Tr          Tw
## CG -0.11570723  0.02314145  0.02314145  0.02314145  0.02314145  0.02314145
## GB  0.02314145 -0.11570723  0.02314145  0.02314145  0.02314145  0.02314145
## TC  0.02314145  0.02314145 -0.11570723  0.02314145  0.02314145  0.02314145
## TG  0.02314145  0.02314145  0.02314145 -0.11570723  0.02314145  0.02314145
## Tr  0.02314145  0.02314145  0.02314145  0.02314145 -0.11570723  0.02314145
## Tw  0.02314145  0.02314145  0.02314145  0.02314145  0.02314145 -0.11570723
## (estimated using likelihood);
## and (mean) root node prior probabilities
## pi =
##        CG        GB        TC        TG        Tr        Tw
## 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
``````
``````## Done.
``````
``````obj<-summary(mtrees)
library(RColorBrewer)
cols<-setNames(brewer.pal(length(levels(ecomorph)),"Accent"),
levels(ecomorph))
nodelabels(pie=obj\$ace,piecol=cols,cex=0.4)
tiplabels(pie=to.matrix(ecomorph,levels(ecomorph)),
piecol=cols,cex=0.3)
## simmap legend?
x<-0.9*par()\$usr
y<-0.9*par()\$usr
x=x,y=y,shape="circle",fsize=0.8)
y<-0.75*par()\$usr
lines(x=x+c(0,150*SCALE),y=rep(y,2))
lines(x=x+rep(0,2),y-c(0,0.01*diff(par()\$usr[3:4])))
lines(x=x+rep(150*SCALE,2),y-c(0,0.01*diff(par()\$usr[3:4])))
text(mean(x+c(0,150*SCALE)),y,"150 mm",pos=1)
`````` Here's a higher quality version: (Click to enlarge.)

You can do anything in R!
{"ft_lang_label":"__label__en","ft_lang_prob":0.5137475,"math_prob":0.9325303,"size":3214,"snap":"2020-45-2020-50","text_gpt3_token_len":1134,"char_repetition_ratio":0.16292834,"word_repetition_ratio":0.075581394,"special_character_ratio":0.41288114,"punctuation_ratio":0.21866667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9648835,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-30T05:03:05Z\",\"WARC-Record-ID\":\"<urn:uuid:efd501be-125d-40d0-8e18-f195963d2451>\",\"Content-Length\":\"266867\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f1207f40-a2d8-49ec-b440-62d7eb018d29>\",\"WARC-Concurrent-To\":\"<urn:uuid:42c3f581-6b8e-4aef-b6b8-a80228bfc19f>\",\"WARC-IP-Address\":\"172.217.9.211\",\"WARC-Target-URI\":\"http://blog.phytools.org/2017/11/even-hackier-solution-to-offsetting-tip.html\",\"WARC-Payload-Digest\":\"sha1:VIVLWWO54XG62UBF3ESA7DRFWRIVYM6P\",\"WARC-Block-Digest\":\"sha1:L7OFQI3RSPHHXRC2BMJXEPPCRAU6AEW7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141205147.57_warc_CC-MAIN-20201130035203-20201130065203-00105.warc.gz\"}"}