Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while plotting a tree with some squirrels using trees package

Tags:

graph

plot

r

tree

I am using the package trees found here, by @jbaums and explained in this post.

My data are the following:

the tree is composed by the trunk

    Trunk
[1] 13.60415

and the branches

Tree
   TreeBranchLength TreeBranchID
1         10.004269            1
2          7.994269            2
3          9.028834           11
4         10.817401           12
5          8.551311          111
6         10.599798          112
7         11.073243          121
8         13.367392          122
9          9.625431         1111
10        10.793569         1112
11         9.896499        11121
12         8.687741        11122
13         7.791180         1211
14        12.506105         1212
15         6.768478         1221
16        10.441796         1222
17        10.751892         1121
18         9.458651         1122
19        10.768509        11221
20        10.150673        11222
21        12.377448       111211
22        12.235136       111212
23         9.074079        11211
24         9.996334        11212
25         9.807019       112221
26        10.895809       112222
27         6.741274      1122211
28        15.841272      1122212
29         5.753920     11222111
30         8.846389     11222112
31        11.925961       112111
32         9.780776       112112
33         8.207965        12221
34        10.079375        12222

the 50 squirrel populations -

Populations
   PopulationPositionOnBranch PopulationBranchID ID
1                  10.6321655             112111  1
2                   1.0644897                  1  2
3                   3.9315473                  1  3
4                   1.0310244                  0  4
5                   9.1768846                  0  5
6                  13.4267181                  0  6
7                   7.9461528                  0  7
8                   6.0533401                121  8
9                   2.1227425                121  9
10                  1.8256787                121 10
11                  4.7332588           11222112 11
12                  4.4837432           11222112 12
13                  4.6200834           11222112 13
14                  2.5622276               1221 14
15                  1.2446683               1221 15
16                  7.0674052                111 16
17                  1.3854674                111 17
18                  4.8735635                111 18
19                  9.5007998               1222 19
20                  6.6373468               1222 20
21                 12.6757728                122 21
22                  4.2685465                122 22
23                  3.9806540                  2 23
24                  3.1025403                  2 24
25                  3.9119065              11122 25
26                  1.5527653              11122 26
27                  1.6687957              11122 27
28                  8.0697456               1122 28
29                  6.7871391               1122 29
30                  9.8050713             111212 30
31                  8.5226920             111212 31
32                  3.6113379             111212 32
33                  7.3184965             111211 33
34                  8.6142984             111211 34
35                  1.3550870               1211 35
36                  8.3650639                 12 36
37                  4.6411446             112112 37
38                  3.2985541             112112 38
39                 12.2344148               1212 39
40                  9.0290776               1212 40
41                  1.3900249               1121 41
42                  0.9261425            1122212 42
43                 15.2522199            1122212 43
44                  4.0253771              12222 44
45                  8.7507678              11222 45
46                  4.6289841            1122211 46
47                  9.1799522                112 47
48                  5.1293838              12221 48
49                  1.1543080              12221 49
50                 10.1014837             112222 50

the code to produce the plot

        g <- germinate(list(trunk.height=Trunk, 
                   branches=Tree$TreeBranchID,
                   lengths=Tree$TreeBranchLength), 
              left='1', right='2', angle=30))

    xy <- squirrels(g, Populations$PopulationBranchID, pos=Populations$PopulationPositionOnBranch, 
               left='1', right='2', pch=21, bg='white', cex=3, lwd=2)
    text(xy$x, xy$y, labels=seq_len(nrow(xy)), font=1)

, which produces

The tree produced

As you can see on the plot bellow population 43 (blue arrow) is out of the tree.. It seems that the length of the branches on the plot do not correspond to the data. For example the branch (left green arrow) on which are populations 38 and 37 is longer than the one where population 43 is (right green arrow), that is not the case in the data. enter image description here What am I doing wrong? Have I understood correctly how to use trees?

like image 781
Sulawesi Avatar asked Mar 21 '15 19:03

Sulawesi


1 Answers

On studying the germinate function it seems to me that the Tree values that you are passing to it needs to be sorted on TreeBranchId field in the ascending order.

The BranchID: 1122212 where you have placed 43 is not the actual 1122212 branch. Due to the order in which you have fed the values in the Tree, the function is somehow messing the location of branch.

I was curious to see if I increase the length of Branch ID: 1122212, will it change the branch where 43 is placed, and guess what? it didn't. The branch which actually showed an increase in length was the branch where you have placed 37 and 38.

So this hint pointed out that something was wrong with germinate function. On further debugging I was able to make it work using the below code.

Tree<-read.csv("treeBranch.csv")
Tree<-Tree[order(Tree$TreeBranchID),] 
g <- germinate(list(trunk.height=15, 
                branches=Tree$TreeBranchID,
                lengths=Tree$TreeBranchLength), 
               left='1', right='2', angle=30)

xy <- squirrels(g, Populations$PopulationBranchID,pos=Populations$PopulationPositionOnBranch, 
            left='1', right='2', pch=21, bg='white', cex=3, lwd=2)
text(xy$x, xy$y, labels=seq_len(nrow(xy)), font=1)
like image 83
Gaurav Sharma Avatar answered Nov 14 '22 16:11

Gaurav Sharma