Saturday, October 21, 2017

plotTree.barplot with multiple stacked barplots & a single tree

A phytools user recently requested the capability to plot multiple stacked barplots next to a phylogeny using plotTree.barplot. In fact, this was almost already possible & required only one tiny change to the code which checks if args.plotTree$plot==FALSE and, if so, sets par(new=TRUE).

It works as follows. Here, I imagine we have three different matrices in which each row of each matrix sums to 1.0 and we want to plot these matrices as three adjacent stacked barplots.

library(phytools)
library(RColorBrewer)
tree
## 
## Phylogenetic tree with 40 tips and 39 internal nodes.
## 
## Tip labels:
##  t39, t40, t10, t6, t18, t19, ...
## 
## Rooted; includes branch lengths.
X
##            [,1]       [,2]       [,3]
## t39 0.525179104 0.10026842 0.37455247
## t40 0.526804250 0.09444652 0.37874923
## t10 0.454712350 0.30239701 0.24289064
## t6  0.692387786 0.10601339 0.20159883
## t18 0.734208612 0.07943336 0.18635802
## t19 0.679013258 0.05800659 0.26298015
## t4  0.259917875 0.60114507 0.13893706
## t5  0.196385441 0.33866046 0.46495410
## t8  0.759235657 0.02862437 0.21213997
## t9  0.398878093 0.26267495 0.33844696
## t22 0.029222105 0.74330209 0.22747581
## t23 0.382022138 0.52175658 0.09622128
## t17 0.106185473 0.40834710 0.48546743
## t26 0.062975924 0.55596673 0.38105735
## t30 0.026656381 0.64656880 0.32677482
## t31 0.043506323 0.61764422 0.33884946
## t29 0.005912221 0.67623489 0.31785289
## t21 0.221507782 0.32307738 0.45541484
## t24 0.125329583 0.43069021 0.44398021
## t25 0.130316467 0.32002782 0.54965571
## t7  0.166474475 0.18771071 0.64581482
## t14 0.408753361 0.10513071 0.48611593
## t15 0.371510701 0.19752121 0.43096809
## t1  0.666583739 0.27400514 0.05941112
## t16 0.414224054 0.41079409 0.17498186
## t37 0.356511404 0.50465671 0.13883189
## t38 0.309634086 0.62894577 0.06142015
## t20 0.391565442 0.53535845 0.07307611
## t11 0.588463079 0.01232121 0.39921571
## t12 0.497284598 0.33453586 0.16817954
## t13 0.204676318 0.47018995 0.32513373
## t35 0.068670118 0.58135820 0.34997168
## t36 0.071858018 0.51778527 0.41035671
## t34 0.031562194 0.53214508 0.43629273
## t27 0.245969069 0.36092619 0.39310474
## t28 0.133252138 0.38461151 0.48213635
## t32 0.226088792 0.32321021 0.45070099
## t33 0.242253847 0.38862081 0.36912534
## t2  0.090611693 0.24371591 0.66567239
## t3  0.669291730 0.30368240 0.02702587
Y
##          [,1]       [,2]       [,3]
## t39 0.6500164 0.25160974 0.09837384
## t40 0.6194750 0.28185245 0.09867253
## t10 0.4784971 0.41797399 0.10352890
## t6  0.4942367 0.03511043 0.47065286
## t18 0.6685693 0.26447364 0.06695705
## t19 0.4566828 0.26926066 0.27405652
## t4  0.3877456 0.27129345 0.34096099
## t5  0.3574238 0.18452804 0.45804811
## t8  0.1877074 0.16769717 0.64459546
## t9  0.3750166 0.07019226 0.55479109
## t22 0.6248713 0.33260262 0.04252609
## t23 0.6133680 0.23298812 0.15364390
## t17 0.6143060 0.01698444 0.36870958
## t26 0.4775323 0.12669048 0.39577720
## t30 0.4885681 0.13927519 0.37215668
## t31 0.5016868 0.17091207 0.32740111
## t29 0.5699250 0.18713634 0.24293865
## t21 0.3558842 0.17479455 0.46932126
## t24 0.3470570 0.17959511 0.47334790
## t25 0.2582049 0.13750900 0.60428611
## t7  0.4851954 0.15034443 0.36446020
## t14 0.7417344 0.05042677 0.20783883
## t15 0.5467123 0.41922590 0.03406176
## t1  0.5785896 0.18257833 0.23883211
## t16 0.3420647 0.04585134 0.61208395
## t37 0.3890263 0.10524413 0.50572959
## t38 0.4055041 0.16297393 0.43152197
## t20 0.3700546 0.11439515 0.51555028
## t11 0.3957279 0.14167762 0.46259447
## t12 0.2746857 0.25512780 0.47018654
## t13 0.3314313 0.53104038 0.13752834
## t35 0.2885883 0.48372431 0.22768738
## t36 0.3702399 0.56026343 0.06949670
## t34 0.4733694 0.47525335 0.05137724
## t27 0.4070432 0.41648865 0.17646819
## t28 0.3493757 0.51506825 0.13555600
## t32 0.3372026 0.51098148 0.15181592
## t33 0.5469213 0.43735383 0.01572492
## t2  0.4490903 0.21609825 0.33481144
## t3  0.4839116 0.46820635 0.04788209
Z
##           [,1]       [,2]       [,3]
## t39 0.37192362 0.02577426 0.60230212
## t40 0.40485849 0.01907763 0.57606388
## t10 0.65818009 0.08511950 0.25670041
## t6  0.44930662 0.44633551 0.10435787
## t18 0.12095062 0.50473687 0.37431251
## t19 0.05888720 0.54257449 0.39853832
## t4  0.39041801 0.03682941 0.57275257
## t5  0.02496900 0.04489223 0.93013876
## t8  0.74564440 0.13866896 0.11568665
## t9  0.71399680 0.13727289 0.14873031
## t22 0.36294795 0.04290760 0.59414445
## t23 0.28190651 0.19496530 0.52312819
## t17 0.34367023 0.05252129 0.60380848
## t26 0.26066189 0.01425516 0.72508295
## t30 0.32023372 0.17417535 0.50559093
## t31 0.39965915 0.13432711 0.46601374
## t29 0.24112467 0.13220168 0.62667365
## t21 0.82423380 0.04924114 0.12652506
## t24 0.30871314 0.55629214 0.13499472
## t25 0.46058285 0.13145529 0.40796185
## t7  0.19828952 0.38930760 0.41240288
## t14 0.34986860 0.13617143 0.51395997
## t15 0.29540064 0.11106646 0.59353290
## t1  0.26258445 0.48054473 0.25687081
## t16 0.30548936 0.25695995 0.43755069
## t37 0.13423430 0.46739358 0.39837212
## t38 0.10208503 0.47486939 0.42304558
## t20 0.11463696 0.44712501 0.43823802
## t11 0.34364773 0.32733844 0.32901383
## t12 0.26557733 0.34336603 0.39105664
## t13 0.20821399 0.23237136 0.55941464
## t35 0.44941577 0.28621676 0.26436747
## t36 0.69454286 0.18320516 0.12225198
## t34 0.65188707 0.29629385 0.05181908
## t27 0.49080474 0.21124254 0.29795272
## t28 0.09286513 0.59173091 0.31540395
## t32 0.14906068 0.59773679 0.25320254
## t33 0.08823629 0.63367537 0.27808834
## t2  0.13526464 0.24478334 0.61995202
## t3  0.07103310 0.36894937 0.56001753
cols<-brewer.pal(3,"YlGnBu")
par(mfrow=c(1,4)) ## we can also use layout
plotTree.barplot(tree,X,add=TRUE,args.barplot=list(xlab="X",col=cols,
    mar=c(5.1,0,2.1,2.1)))
plotTree.barplot(tree,Y,args.barplot=list(xlab="Y",mar=c(5.1,0,2.1,2.1),
    col=cols),args.plotTree=list(plot=FALSE),add=TRUE)
plotTree.barplot(tree,Z,args.barplot=list(xlab="Z",mar=c(5.1,0,2.1,2.1),
    col=cols),args.plotTree=list(plot=FALSE),add=TRUE)

plot of chunk unnamed-chunk-1

Neat.

Data were simulated as follows:

tree<-pbtree(n=40,scale=1)
X<-fastBM(tree,nsim=3,bounds=c(0,Inf))
Y<-fastBM(tree,nsim=3,bounds=c(0,Inf))
Z<-fastBM(tree,nsim=3,bounds=c(0,Inf))
X<-X/rowSums(X)
Y<-Y/rowSums(Y)
Z<-Z/rowSums(Z)

1 comment:

  1. Hi Liam, this is a great function! It is posible to add the scale of the tree? or how can I depicts the tree scale? Thanks in advance

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.