R/geom_taxalink.R
0a97b0e8
 ##' link between taxa
 ##'
 ##' 
 ##' @title geom_taxalink
 ##' @param taxa1 taxa1, can be label or node number
 ##' @param taxa2 taxa2, can be label or node number
 ##' @param curvature A numeric value giving the amount of curvature.
 ##' Negative values produce left-hand curves,
 ##' positive values produce right-hand curves, and zero produces a straight line.
 ##' @param ... additional parameter
 ##' @return ggplot layer
 ##' @export
 ##' @importFrom ggplot2 GeomCurve
 ##' @author Guangchuang Yu
 geom_taxalink <- function(taxa1, taxa2, curvature=0.5, ...) {
     position = "identity"
     show.legend = NA
     na.rm = TRUE
     inherit.aes = FALSE    
 
c1e35f48
     mapping <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
0a97b0e8
 
     layer(stat=StatTaxalink,
c1e35f48
           mapping=mapping,
0a97b0e8
           geom=GeomCurve,
           position='identity',
           show.legend=show.legend,
           inherit.aes = inherit.aes,
           params = list(taxa1 = taxa1,
                         taxa2 = taxa2,
                         curvature = curvature,
                         na.rm = na.rm,
bedefde1
                         ...),
da0f0f7f
           if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
0a97b0e8
           )
 }
 
 
 StatTaxalink <- ggproto("StatTaxalink", Stat,
                         compute_group = function(self, data, scales, params, taxa1, taxa2) {
3ba777c5
                             node1 <- taxa2node(data, taxa1)
                             node2 <- taxa2node(data, taxa2)
0a97b0e8
                             x <- data$x
                             y <- data$y
                             
                             data.frame(x = x[node1],
                                        xend = x[node2],
                                        y = y[node1],
                                        yend = y[node2])
                             
                         },
                         required_aes = c("x", "y", "xend", "yend")
                         )