Skip to content

Commit

Permalink
🦃 Add ggplot2 method for grouped df
Browse files Browse the repository at this point in the history
*gobble* fixes #2351 *gobble*
  • Loading branch information
hadley committed Nov 30, 2017
1 parent caca650 commit 14fd283
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -37,6 +37,7 @@ S3method(fortify,summary.glht)
S3method(fortify,tbl)
S3method(ggplot,data.frame)
S3method(ggplot,default)
S3method(ggplot,grouped_df)
S3method(ggplot_add,"NULL")
S3method(ggplot_add,Coord)
S3method(ggplot_add,Facet)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Expand Up @@ -242,6 +242,10 @@ up correct aspect ratio, and draws a graticule.

* `fortify()` gains a method for tbls (@karawoo, #2218)

* `ggplot` gains a method for `grouped_df`s that adds a `.group` variable,
which computes a unique value for each group, and automatically sets
the group aesthetic to use that variable (#2351).

* `ggproto()` produces objects with class `c("ggproto", "gg")`, allowing for
a more informative error message when adding layers, scales, or other ggproto
objects (@jrnold, #2056).
Expand Down
10 changes: 10 additions & 0 deletions R/plot.r
Expand Up @@ -106,6 +106,16 @@ ggplot.data.frame <- function(data, mapping = aes(), ...,
p
}

#' @export
ggplot.grouped_df <- function(data, mapping = aes(), ...,
environment = parent.frame()) {

data$.group <- dplyr::group_indices(data)
mapping$group <- mapping$group %||% quote(.group)

ggplot.data.frame(data, mapping = mapping, ..., environment = environment)
}

plot_clone <- function(plot) {
p <- plot
p$scales <- plot$scales$clone()
Expand Down

0 comments on commit 14fd283

Please sign in to comment.