R/geom_text.R
6c9cd355
 ##' geom_text2 support aes(subset) via setup_data
 ##'
102483c0
 ##'
6c9cd355
 ##' @title geom_text2
1d4d6914
 ##' @param mapping the aesthetic mapping
 ##' @param data A layer specific dataset -
 ##'             only needed if you want to override he plot defaults.
 ##' @param position The position adjustment to use for overlapping points on this layer
275491b1
 ##' @param family sans by default, can be any supported font
1d4d6914
 ##' @param parse if TRUE, the labels will be passd into expressions
6c9cd355
 ##' @param na.rm logical
 ##' @param show.legend logical
 ##' @param inherit.aes logical
1d4d6914
 ##' @param ... other arguments passed on to 'layer'
6c9cd355
 ##' @param nudge_x horizontal adjustment
 ##' @param nudge_y vertical adjustment
 ##' @param check_overlap if TRUE, text that overlaps previous text in the same layer will not be plotted
e4ea056a
 ##' @return text layer
 ##' @importFrom ggplot2 layer
 ##' @importFrom ggplot2 position_nudge
275491b1
 ##' @importFrom ggplot2 aes_string
e4ea056a
 ##' @export
1d4d6914
 ##' @seealso
 ##' \link[ggplot2]{geom_text}
e4ea056a
 ##' @author Guangchuang Yu
7d9641c6
 geom_text2 <- function(mapping = NULL, data = NULL,
275491b1
                        position = "identity", family="sans", parse = FALSE,
                        na.rm=TRUE, show.legend = NA, inherit.aes = TRUE,
e4ea056a
                        ..., nudge_x = 0, nudge_y = 0, check_overlap = FALSE) {
 
     if (!missing(nudge_x) || !missing(nudge_y)) {
         if (!missing(position)) {
             stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE)
         }
102483c0
 
e4ea056a
         position <- position_nudge(nudge_x, nudge_y)
     }
102483c0
 
e4ea056a
     default_aes <- aes_(node=~node)
     if (is.null(mapping)) {
         mapping <- default_aes
     } else {
         mapping <- modifyList(mapping, default_aes)
     }
102483c0
 
275491b1
     if (parse == "emoji") {
         label_aes <- aes_string(label=paste0("suppressMessages(emoji(", as.list(mapping)$label,"))"))
         mapping <- modifyList(mapping, label_aes)
         emoji <- get_fun_from_pkg("emojifont", "emoji")
         parse <- FALSE
         family <- "OpenSansEmoji"
     }
 
e4ea056a
     layer(
         data = data,
         mapping = mapping,
7d9641c6
         stat = StatTreeData,
e4ea056a
         geom = GeomTextGGtree,
         position = position,
         show.legend = show.legend,
         inherit.aes = inherit.aes,
         params = list(
275491b1
             parse = parse,
             family = family,
             check_overlap = check_overlap,
             na.rm = na.rm,
             ...
bedefde1
         ),
0d9e240e
         check.aes = FALSE
e4ea056a
     )
 }
1d4d6914
 
6c9cd355
 
 ##' @importFrom ggplot2 GeomText
 ##' @importFrom ggplot2 draw_key_text
 GeomTextGGtree <- ggproto("GeomTextGGtree", GeomText,
                           setup_data = function(data, params) {
e4ea056a
                               if (is.null(data$subset))
                                   return(data)
275491b1
                               data[which(data$subset),] ## use `which` makes it compatible with NA
6c9cd355
                           },
102483c0
                           ## compute_group = function(data, params) {
                           ##     data
                           ## },
6c9cd355
                           draw_panel = function(data, panel_scales, coord, parse = FALSE,
e4ea056a
                               na.rm = TRUE, check_overlap = FALSE) {
6c9cd355
                               GeomText$draw_panel(data, panel_scales, coord, parse,
                                                   na.rm, check_overlap)
                           },
e4ea056a
                           required_aes = c("node", "x", "y", "label"),
102483c0
 
6c9cd355
                           default_aes = aes(colour = "black", size = 3.88, angle = 0, hjust = 0.5,
                               vjust = 0.5, alpha = NA, family = "", fontface = 1, lineheight = 1.2),
102483c0
 
6c9cd355
                           draw_key = draw_key_text
                           )
e4ea056a
 
7d9641c6
 StatTreeData <-  ggproto("StatTreeLabel", Stat,
                          required_aes = "node",
                          compute_group = function(data, scales) {
                              setup_tree_data(data)
                          }
                          )
e4ea056a