Friday, August 26, 2016

Tracking CRAN package downloads and connectivity

I recently discovered this method of visualizing past & future CRAN downloads by Péter Sólymos. The following is a highly simplied version showing only past CRAN downloads:

library(cranlogs)
daily.downloads<-function(pkg,from="2013-08-27",to="2016-08-26"){
    obj<-cran_downloads(pkg,from=from,to=to)
    attach(obj)
    plot(count~date,type="l",col="grey",
        ylim=c(0,quantile(count,0.99)),
        ylab=paste("daily downloads (",pkg,")",sep=""))
    lines(lowess(date,count),col="red",lwd=2)
    title(main=pkg)
    detach(obj)
    invisible(obj)
}

Naturally (and my apologies for the narcissism involved here), I had to apply it to phytools:

daily.downloads("phytools")

plot of chunk unnamed-chunk-2

Here are some other popular R phylogenetics packages:

daily.downloads("ape")

plot of chunk unnamed-chunk-3

daily.downloads("phangorn")

plot of chunk unnamed-chunk-3

daily.downloads("geiger")

plot of chunk unnamed-chunk-3

and less-popular ones, like our Rphylip package:

daily.downloads("Rphylip")

plot of chunk unnamed-chunk-4

Note that (and not with specific reference to Rphylip by any means) some excellent packages are not as often downloaded, simply because they perform highly specialized analyses or because they have dependency relationships with relatively few other packages.

BTW, here is a plot made with the package miniCRAN (itself using igraph) which summarizes the dependency packages of a given library (that is, the packages on which it depends or imports from, not the reverse):

library(miniCRAN)
obj<-makeDepGraph("phytools",enhances=TRUE)
plot(obj,legendPosition=c(-1,1),vertex.size=20,cex=0.6)

plot of chunk unnamed-chunk-5

Updates to tip label printing and summary method for "cophylo" objects

I just pushed a couple of small updates to the object class "cophylo" plotting method - in particular pertaining to the spacing of tip labels and what to do with underscores in species label names.

First, I recently discovered that (accidentally) by using sub instead of gsub, I was substituting only the first underscore character "_" for a space character " " in species names, instead of all such characters.

This was very easy to fix as gsub works the same way as sub, except that it substitutes all instances of a character pattern as desired, instead of just the first encountered.

Next, however, I realized that the width in a plotting device of "_" and " " differ by more than a factor of two (although this may vary between device types). E.g.:

plot.window(xlim=c(0,1),ylim=c(0,1))
plot.new()

plot of chunk unnamed-chunk-1

strwidth("_")
## [1] 0.02792554
strwidth(" ")
## [1] 0.01196809

but in computing where to draw the links to tip labels, I had been using strwidth(tree$tip.label) rather than strwidth(gsub("_"," ",tree$tip.label)).

Finally, I have made some changes to the S3 summary method for the object class "cophylo" so that the association matrix prints more nicely! In particular, I tried to space the columns by the longest name in the matrix, rather than just by using tabs.

So, for instance, here is the old methods:

library(phytools)
t1<-read.tree(text="(A_B_C,(C_D_E_F_G_H_I_J_K_L,D_E_F));")
t2<-read.tree(text="(C_D_E_F_G_H_I_J_K_L,(A_B_C,D_E_F));")
obj<-cophylo(t1,t2)
## Rotating nodes to optimize matching...
## Done.
summary(obj)
## 
## Co-phylogenetic ("cophylo") object: obj 
## 
## Tree 1 (left tree) is an object of class "phylo" containing 3 species.
## 
## Tree 2 (right tree) is an object of class "phylo" containing 3 species.
## 
## Association (assoc) table as follows:
## 
##  left:   ----    right:
##   A_B_C  ----     A_B_C 
##   C_D_E_F_G_H_I_J_K_L    ----     C_D_E_F_G_H_I_J_K_L 
##   D_E_F  ----     D_E_F
plot(obj,link.type="curved")

plot of chunk unnamed-chunk-2

This problem of spacing is particularly apparent, I think, if we shrink the font size:

plot(obj,link.type="curved",fsize=0.6)

plot of chunk unnamed-chunk-3

Now, let's load the updated source code from GitHub:

source("https://raw.githubusercontent.com/liamrevell/phytools/59f2e02a83f9142d480ad49f7e226ca0d28aeeb6/R/cophylo.R")
summary(obj)
## 
## Co-phylogenetic ("cophylo") object: obj 
## 
## Tree 1 (left tree) is an object of class "phylo" containing 3 species.
## 
## Tree 2 (right tree) is an object of class "phylo" containing 3 species.
## 
## Association (assoc) table as follows:
## 
##  left:               ----    right:
##  A_B_C               ----    A_B_C
##  C_D_E_F_G_H_I_J_K_L ----    C_D_E_F_G_H_I_J_K_L
##  D_E_F               ----    D_E_F
plot(obj,link.type="curved")

plot of chunk unnamed-chunk-4

plot(obj,link.type="curved",fsize=0.6)

plot of chunk unnamed-chunk-4

Much better. phytools can also be updated to its latest version from GitHub using devtools.