Skip to content

Commit 7881a3d

Browse files
committed
Fix draw_panel confusion
Fixes #1587
1 parent dd4fbf1 commit 7881a3d

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

vignettes/extending-ggplot2.Rmd

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ This is very similar to defining a new stat. You always need to provide fields/m
364364
* `draw_key` provides the function used to draw the key in the legend.
365365
You can see a list of all the build in key functions in `?draw_key`
366366

367-
* `draw_group()` is where the magic happens. This function takes three
367+
* `draw_panel()` is where the magic happens. This function takes three
368368
arguments and returns a grid grob. It is called once for each panel.
369369
It's the most complicated part and is described in more detail below.
370370

371-
`draw_group()` has three arguments:
371+
`draw_panel()` has three arguments:
372372

373373
* `data`: a data frame with one column for each aesthetic.
374374

@@ -381,7 +381,7 @@ Generally you won't use `panel_scales` and `coord` directly, but you will always
381381

382382
### Collective geoms
383383

384-
Overriding `draw_panel()` is most appropriate if there is one graphic element per row. In other cases, you want graphic element per group. For example, take polygons: each row gives one vertex of a polygon. In this case, you should instead override `draw_group()`:
384+
Overriding `draw_panel()` is most appropriate if there is one graphic element per row. In other cases, you want graphic element per group. For example, take polygons: each row gives one vertex of a polygon. In this case, you should instead override `draw_group()`.
385385

386386
The following code makes a simplified version of `GeomPolygon`:
387387

@@ -433,12 +433,8 @@ ggplot(mpg, aes(displ, hwy)) +
433433

434434
There are a few things to note here:
435435

436-
* We override `draw_group()` instead of `draw_layer()` because we want
437-
one polygon per group, not one polygon per row. If you look at the source
438-
code for the original `GeomPolygon` you'll see it actually overrides
439-
`geom_layer()` because it uses some tricks to make `polygonGrob()` produce
440-
multiple polygons in one call. This is considerably more complicated, but
441-
gives better performance.
436+
* We override `draw_group()` instead of `draw_panel()` because we want
437+
one polygon per group, not one polygon per row.
442438

443439
* If the data contains two or fewer points, there's no point trying to draw
444440
a polygon, so we return a `nullGrob()`. This is the graphical equivalent
@@ -450,6 +446,8 @@ There are a few things to note here:
450446
change it there). `lwd` is measured in points, but ggplot2 uses mm,
451447
so we need to multiply it by the adjustment factor `.pt`.
452448

449+
You might want to compare this to the real `GeomPolygon`. You'll see it overrides `draw_panel()` because it uses some tricks to make `polygonGrob()` produce multiple polygons in one call. This is considerably more complicated, but gives better performance.
450+
453451
### Inheriting from an existing Geom
454452

455453
Sometimes you just want to make a small modification to an existing geom. In this case, rather than inheriting from `Geom` you can inherit from an existing subclass. For example, we might want to change the defaults for `GeomPolygon` to work better with `StatChull`:

0 commit comments

Comments
 (0)