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?
The reshaping of this might take a while, but this is broadly possible. E.g., rejigging your data representation so it looks like:
And manually moving the tree parts around in
tkplot
, you can get:Doing this automatically is a whole different story admittedly.
A version that supports trees with more than two branches. A bit work is required to convert to a data.tree structure, and to add the squirrels to it. But once you're there, the plotting is straight forward.
Looking at the data:
This prints like so:
Once we're here, plotting is also easy:
I probably over-thought this, but... squirrels.
And now we plot.
And for kicks we will simulate a bigger tree (and put some apples on it like in Farmville):
I put a bit more thought/time into this, and have packaged up some horticultural functions in package
trees
, here.With
trees
, you can:seed()
;germinate()
;foliate()
;squirrels()
; andprune()
the tree.Let's fertilise a seed and grow a tree
And add some leaves
Or some squirrels
Plotting @Remi.b's tree and squirrels
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).
Well, you could convert your data to define a "tree" as defined by the
ape
package. Here's a function that can convert your data.frame to the correct format.Then we can plot it somewhat easily
Now, if we want to add the squirrel information, we need to know where each branch is located. I'm going to borrow
getphylo_x
andgetphylo_y
from this answer. Then I can runto add the squirrel information to the plot. The final result is
It's not perfect but it's not a bad start.