tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.51k stars 2.02k forks source link

setup_data() method fail in geom_text #1380

Closed GuangchuangYu closed 9 years ago

GuangchuangYu commented 9 years ago

subsetting is commonly used in visualizing phylogenetic tree.

With the new feature of setup_data() function, I have sucessfully over-write the method for performing subsetting with previous github version of ggplot2.

Here is my implementation, which support aes(subset=X)


geom_text2 <- function(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", parse = FALSE, show.legend = NA, inherit.aes = TRUE,
  ..., 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)
    }

    position <- position_nudge(nudge_x, nudge_y)
  }

  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomTextGGtree,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      parse = parse,
      check_overlap = check_overlap,
      ...
    )
  )
}

##' @importFrom ggplot2 GeomText
##' @importFrom ggplot2 draw_key_text
GeomTextGGtree <- ggproto("GeomTextGGtree", GeomText,
                          setup_data = function(data, params) {
                              data[data$subset,]
                          },

                          draw_panel = function(data, panel_scales, coord, parse = FALSE,
                              na.rm = FALSE, check_overlap = FALSE) {
                              GeomText$draw_panel(data, panel_scales, coord, parse,
                                                  na.rm, check_overlap)
                          },

                          required_aes = c("x", "y", "label"),

                          default_aes = aes(colour = "black", size = 3.88, angle = 0, hjust = 0.5,
                              vjust = 0.5, alpha = NA, family = "", fontface = 1, lineheight = 1.2),

                          draw_key = draw_key_text
                          )

I update ggplot2 today and found that my implementation fail to work.

It do work for segment, but fail for both text and point.

ggtree(rtree(30)) + geom_segment2(aes(subset=isTip, x=x+1, yend=y), xend=10)

screenshot 2015-10-20 17 24 15

> ggtree(rtree(30)) + geom_segment2(aes(subset=isTip, x=x+1, yend=y), xend=10) + geom_text2(aes(subset=isTip, label=label))
Error: is.logical(na.rm) is not TRUE
> traceback()
13: stop(sprintf(ngettext(length(r), "%s is not TRUE", "%s are not all TRUE"),
        ch), call. = FALSE, domain = NA)
12: stopifnot(is.logical(na.rm))
11: remove_missing(data, params$na.rm, c(self$required_aes, self$non_missing_aes),
        snake_class(self))
10: f(..., self = self)
9: self$geom$handle_na(data, self$geom_params)
8: f(..., self = self)
7: l$draw_geom(d, panel, plot$coordinates)
6: (function (l, d)
   l$draw_geom(d, panel, plot$coordinates))(dots[[1L]][[4L]], dots[[2L]][[4L]])
5: mapply(FUN = f, ..., SIMPLIFY = FALSE)
4: Map(function(l, d) l$draw_geom(d, panel, plot$coordinates), plot$layers,
       data)
3: ggplot_gtable(data)
2: print.ggplot(x)
1: function (x, ...)
   UseMethod("print")(x)
hadley commented 9 years ago

You need to always pass na.rm in the layer() args - you might want to re-look at the extending ggplot2 vignette.

GuangchuangYu commented 9 years ago

Thank you!