R/geom_point.R
f1f0d8d2
 
14435e79
 ##' add tip point
 ##'
4edbfa25
 ##'
14435e79
 ##' @title geom_tippoint
f1f0d8d2
 ##' @inheritParams geom_point2
14435e79
 ##' @return tip point layer
 ##' @export
 ##' @author Guangchuang Yu
4edbfa25
 geom_tippoint <- function(mapping = NULL, data = NULL,
f1f0d8d2
                        position = "identity", na.rm = FALSE,
                           show.legend = NA, inherit.aes = TRUE, ...) {
14435e79
     isTip <- NULL
f1f0d8d2
     self_mapping <- aes(subset = isTip)
     if (is.null(mapping)) {
         mapping <- self_mapping
     } else {
02392a7a
         mapping <- modifyList(self_mapping, mapping)
f1f0d8d2
     }
4edbfa25
     geom_point2(mapping, data, position, na.rm, show.legend, inherit.aes, ...)
14435e79
 }
 
0ce35569
 ## angle is not supported,
 ## https://github.com/GuangchuangYu/ggtree/issues/77
 ##
 ##
 ## geom_tippoint2 <- function(mapping=NULL, hjust=0, ...) {
 ##     angle <- NULL
 ##     isTip <- NULL
 ##     m1 <- aes(subset=(isTip & (angle < 90 | angle > 270)), angle=angle)
 ##     m2 <- aes(subset=(isTip & (angle >= 90 & angle <=270)), angle=angle+180)
02392a7a
 
0ce35569
 ##     if (!is.null(mapping)) {
 ##         m1 <- modifyList(mapping, m1)
 ##         m2 <- modifyList(mapping, m2)
 ##     }
02392a7a
 
0ce35569
 ##     list(geom_tippoint(m1, hjust=hjust, ...),
 ##          geom_tippoint(m2, hjust=1-hjust, ...)
 ##          )
 ## }
02392a7a
 
 
14435e79
 ##' add node point
 ##'
4edbfa25
 ##'
14435e79
 ##' @title geom_nodepoint
f1f0d8d2
 ##' @inheritParams geom_point2
14435e79
 ##' @return node point layer
 ##' @export
 ##' @author Guangchuang Yu
4edbfa25
 geom_nodepoint <- function(mapping = NULL, data = NULL,
f1f0d8d2
                        position = "identity", na.rm = FALSE,
                        show.legend = NA, inherit.aes = TRUE, ...) {
14435e79
     isTip <- NULL
f1f0d8d2
     self_mapping <- aes(subset = !isTip)
     if (is.null(mapping)) {
         mapping <- self_mapping
     } else {
         mapping %<>% modifyList(self_mapping)
     }
4edbfa25
     geom_point2(mapping, data, position, na.rm, show.legend, inherit.aes, ...)
14435e79
 }
 
 
 ##' add root point
 ##'
4edbfa25
 ##'
14435e79
 ##' @title geom_rootpoint
f1f0d8d2
 ##' @inheritParams geom_point2
14435e79
 ##' @return root point layer
 ##' @export
 ##' @author Guangchuang Yu
4edbfa25
 geom_rootpoint <- function(mapping = NULL, data = NULL,
f1f0d8d2
                            position = "identity", na.rm = FALSE,
                            show.legend = NA, inherit.aes = TRUE, ...) {
14435e79
     isTip <- node <- parent <- NULL
f1f0d8d2
     self_mapping <- aes(subset = (node == parent))
     if (is.null(mapping)) {
         mapping <- self_mapping
     } else {
         mapping %<>% modifyList(self_mapping)
     }
f35d40fd
     geom_point2(mapping, data, position, na.rm, show.legend, inherit.aes, ...)
14435e79
 }
 
 
f1f0d8d2
 ##' geom_point2 support aes(subset) via setup_data
 ##'
4edbfa25
 ##'
f1f0d8d2
 ##' @title geom_point2
 ##' @param mapping aes mapping
 ##' @param data data
 ##' @param position position
 ##' @param na.rm logical
 ##' @param show.legend logical
 ##' @param inherit.aes logical
 ##' @param ... addktional parameter
 ##' @importFrom ggplot2 layer
 ##' @export
 ##' @seealso
 ##' \link[ggplot2]{geom_point}
 ##' @return point layer
 ##' @author Guangchuang Yu
f35d40fd
 geom_point2 <- function(mapping = NULL, data = NULL,
f1f0d8d2
                        position = "identity", na.rm = FALSE,
                        show.legend = NA, inherit.aes = TRUE, ...) {
90df068e
 
4edbfa25
 
90df068e
     default_aes <- aes_(node=~node)
     if (is.null(mapping)) {
         mapping <- default_aes
     } else {
         mapping <- modifyList(mapping, default_aes)
     }
4edbfa25
 
90df068e
     layer(
         data = data,
         mapping = mapping,
f35d40fd
         stat = StatTreeData,
90df068e
         geom = GeomPointGGtree,
         position = position,
         show.legend = show.legend,
         inherit.aes = inherit.aes,
         params = list(
             na.rm = na.rm,
             ...
a92aaed1
         ),
7ca12fe1
         if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
f1f0d8d2
     )
 }
 
 ##' @importFrom ggplot2 ggproto
 ##' @importFrom ggplot2 GeomPoint
 ##' @importFrom ggplot2 draw_key_point
 GeomPointGGtree <- ggproto("GeomPointGGtree", GeomPoint,
                            setup_data = function(data, params) {
90df068e
                                if (is.null(data$subset))
                                    return(data)
f1f0d8d2
                                data[data$subset,]
4edbfa25
                            }
 
                            ## ,
 
90df068e
                            ## draw_panel = function(data, panel_scales, coord, na.rm = FALSE){
                            ##     GeomPoint$draw_panel(data, panel_scales, coord, na.rm)
                            ## },
4edbfa25
 
90df068e
                            ## draw_key = draw_key_point,
4edbfa25
 
90df068e
                            ## required_aes = c("x", "y"),
                            ## default_aes = aes(shape = 19, colour = "black", size = 1.5, fill = NA,
                            ##                   alpha = NA, stroke = 0.5)
f1f0d8d2
                             )
90df068e