Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot a tree (and squirrels) in R?

Here is my tree:

tree = data.frame(branchID = c(1,11,12,111,112,1121,1122), length = c(32, 21, 19, 5, 12, 6, 2))  > tree   branchID length 1        1     32 2       11     21 3       12     19 4      111      5 5      112     12 6     1121      6 7     1122      2 

This tree is in 2D and is made of branches. Each branch has an ID. 1 is the trunk. Then the trunk bifurcate into two branches, 11 on the left and 12 on the right. 11 bifurcates as well in the branches called 111 (going toward the left) and 112 (going toward the right). etc.. Each branch has a certain length.

On this tree there are squirrels:

squirrels = data.frame(branchID = c(1,11,1121,11,111), PositionOnBranch = c(23, 12, 4, 2, 1), name=c("FluffyTail", "Ginger", "NutCracker", "SuperSquirrel", "ChipnDale"))  > squirrels   branchID PositionOnBranch          name 1        1               23    FluffyTail 2       11               12        Ginger 3     1121                4    NutCracker 4       11                2 SuperSquirrel 5      111                1     ChipnDale 

Each squirrel is found on a specific branch. For example the FluffyTail is on the trunk at position 23 (the total length of the trunk being 32). ChipnDale is on the branch 111 at position 1 (the total length of the branch 111 is 5). The position is taken relatively to the lower extremity of the branch.

How can I plot my tree and my squirrels?

like image 595
Remi.b Avatar asked Jan 27 '15 05:01

Remi.b


1 Answers

I put a bit more thought/time into this, and have packaged up some horticultural functions in package trees, here.

With trees, you can:

  • generate a random tree design (a random seed, so to speak) with seed();
  • sow the seed to give rise to a magnificent tree with germinate();
  • add randomly-located leaves (or squirrels) with foliate();
  • add squirrels (for example) to specified locations with squirrels(); and
  • prune() the tree.

# Install the package and set the RNG state devtools::install_github('johnbaums/trees') set.seed(1) 

Let's fertilise a seed and grow a tree

# Create a tree seed     s <- seed(70, 10, min.branch.length=0, max.branch.length=4,           min.trunk.height=5, max.trunk.height=8)  head(s, 10)  #       branch    length # 1          0 6.3039785 # 2          L 2.8500587 # 3         LL 1.5999775 # 4        LLL 1.3014086 # 5       LLLL 3.0283486 # 6      LLLLL 0.8107690 # 7     LLLLLR 2.8444849 # 8    LLLLLRL 0.4867677 # 9   LLLLLRLR 0.9819541 # 10 LLLLLRLRR 0.5732175  # Germinate the seed g <- germinate(s, col='peachpuff4') 

enter image description here

And add some leaves

leafygreens <- colorRampPalette(paste0('darkolivegreen', c('', 1:4)))(100) foliate(g, 5000, 4, pch=24:25, col=NA, cex=1.5, bg=paste0(leafygreens, '30')) 

enter image description here

Or some squirrels

plot(g, col='peachpuff4') squirrels(g,            branches=c("LLLLRRRL", "LRLRR", "LRRLRLLL", "LRRRLL", "RLLLLLR",                       "RLLRL", "RLLRRLRR", "RRRLLRL", "RRRLLRR", "RRRRLR"),           pos=c(0.22, 0.77, 0.16, 0.12, 0.71, 0.23, 0.18, 0.61, 0.8, 2.71),           pch=20, cex=2.5) 

enter image description here

Plotting @Remi.b's tree and squirrels

g <- germinate(list(trunk.height=32,                     branches=c(1, 2, 11, 12, 121, 122),                    lengths=c(21, 19, 5, 12, 6, 2)),                left='1', right='2', angle=40)  xy <- squirrels(g, c(0, 1, 121, 1, 11), pos=c(23, 12, 4, 2, 1),                 left='1', right='2', pch=21, bg='white', cex=3, lwd=2) text(xy$x, xy$y, labels=seq_len(nrow(xy)), font=2) legend('bottomleft', bty='n',       legend=paste(seq_len(nrow(xy)),                     c('FluffyTail', 'Ginger', 'NutCracker', 'SuperSquirrel',                       'ChipnDale'), sep='. ')) 

enter image description here


EDIT:

Following @baptiste's hot tip about @ScottChamberlain's rphylopic package, it's time to upgrade those dots to squirrels (though they may resemble coffee beans).

library(rphylopic) s <- seed(50, 10, min.branch.length=0, max.branch.length=5,           min.trunk.height=5, max.trunk.height=8) g <- germinate(s, trunk.width=15, col='peachpuff4') leafygreens <- colorRampPalette(paste0('darkolivegreen', c('', 1:4)))(100) foliate(g, 2000, 4, pch=24:25, col=NA, cex=1.2, bg=paste0(leafygreens, '50')) xy <- foliate(g, 2, 2, 4, xy=TRUE, plot=FALSE)  # snazzy drop shadow add_phylopic_base(     image_data("5ebe5f2c-2407-4245-a8fe-397466bb06da", size = "64")[[1]],      1, xy$x, xy$y, ysize = 2.3, col='black') add_phylopic_base(     image_data("5ebe5f2c-2407-4245-a8fe-397466bb06da", size = "64")[[1]],      1, xy$x, xy$y, ysize = 2, col='darkorange3') 

enter image description here

like image 102
jbaums Avatar answered Sep 23 '22 02:09

jbaums