Skip to content

Commit 5e9b688

Browse files
authored
Reduce run-time of long examples (#2575)
* Reduce run time of vars() examples * Reduce run times of other long examples To ensure they take less than 5s
1 parent ef603ea commit 5e9b688

13 files changed

+48
-165
lines changed

R/facet-.r

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ Facet <- ggproto("Facet", NULL,
173173
#' Quote faceting variables
174174
#'
175175
#' @description
176-
#'
177176
#' Just like [aes()], `vars()` is a [quoting function][rlang::quotation]
178177
#' that takes inputs to be evaluated in the context of a dataset.
179178
#' These inputs can be:
@@ -192,20 +191,19 @@ Facet <- ggproto("Facet", NULL,
192191
#' @seealso [aes()], [facet_wrap()], [facet_grid()]
193192
#' @export
194193
#' @examples
195-
#' p <- ggplot(diamonds) + geom_point(aes(carat, price))
196-
#' p + facet_wrap(vars(cut, clarity))
194+
#' p <- ggplot(mtcars, aes(wt, disp)) + geom_point()
195+
#' p + facet_wrap(vars(vs, am))
197196
#'
198197
#' # vars() makes it easy to pass variables from wrapper functions:
199198
#' wrap_by <- function(...) {
200199
#' facet_wrap(vars(...), labeller = label_both)
201200
#' }
202-
#' p + wrap_by(cut)
203-
#' p + wrap_by(cut, clarity)
204-
#'
201+
#' p + wrap_by(vs)
202+
#' p + wrap_by(vs, am)
205203
#'
206204
#' # You can also supply expressions to vars(). In this case it's often a
207205
#' # good idea to supply a name as well:
208-
#' p + wrap_by(depth = cut_number(depth, 3))
206+
#' p + wrap_by(drat = cut_number(drat, 3))
209207
#'
210208
#' # Let's create another function for cutting and wrapping a
211209
#' # variable. This time it will take a named argument instead of dots,
@@ -225,7 +223,7 @@ Facet <- ggproto("Facet", NULL,
225223
#' }
226224
#'
227225
#' # Thanks to tidy eval idioms we now have another useful wrapper:
228-
#' p + wrap_cut(depth)
226+
#' p + wrap_cut(drat)
229227
vars <- function(...) {
230228
rlang::quos(...)
231229
}

R/facet-grid-.r

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ NULL
6262
#' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
6363
#'
6464
#' # Use vars() to supply variables from the dataset:
65-
#' p + facet_grid(vars(drv))
65+
#' p + facet_grid(rows = vars(drv))
6666
#' p + facet_grid(cols = vars(cyl))
6767
#' p + facet_grid(vars(drv), vars(cyl))
6868
#'
6969
#' # The historical formula interface is also available:
70+
#' \donttest{
7071
#' p + facet_grid(. ~ cyl)
7172
#' p + facet_grid(drv ~ .)
7273
#' p + facet_grid(drv ~ cyl)
74+
#' }
7375
#'
7476
#' # To change plot order of facet grid,
7577
#' # change the order of variable levels with factor()
@@ -99,50 +101,17 @@ NULL
99101
#' facet_grid(manufacturer ~ ., scales = "free", space = "free") +
100102
#' theme(strip.text.y = element_text(angle = 0))
101103
#'
102-
#' # Facet labels ------------------------------------------------------
103-
#' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
104-
#' p
105-
#'
106-
#' # label_both() displays both variable name and value
107-
#' p + facet_grid(vars(vs), vars(cyl), labeller = label_both)
108-
#'
109-
#' # With vars() it is easy to pass custom names for the faceting groups:
110-
#' p + facet_grid(vars(`V/S` = vs), vars(cylinder = cyl), labeller = label_both)
111-
#'
112-
#' # label_parsed() parses text into mathematical expressions, see ?plotmath
113-
#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)"))
114-
#' ggplot(mtcars, aes(wt, mpg)) +
115-
#' geom_point() +
116-
#' facet_grid(. ~ cyl2, labeller = label_parsed)
117-
#'
118-
#' # label_bquote() makes it easy to construct math expressions
119-
#' p + facet_grid(. ~ vs, labeller = label_bquote(cols = alpha ^ .(vs)))
120-
#'
121-
#' # The facet strips can be displayed near the axes with switch
122-
#' data <- transform(mtcars,
123-
#' am = factor(am, levels = 0:1, c("Automatic", "Manual")),
124-
#' gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five"))
125-
#' )
126-
#' p <- ggplot(data, aes(mpg, disp)) + geom_point()
127-
#' p + facet_grid(am ~ gear, switch = "both")
128-
#' # It looks better without boxes around the strips
129-
#' p + facet_grid(am ~ gear, switch = "both") +
130-
#' theme(strip.background = element_blank())
131-
#'
132104
#' # Margins ----------------------------------------------------------
133105
#' \donttest{
134106
#' # Margins can be specified by logically (all yes or all no) or by specific
135107
#' # variables as (character) variable names
136108
#' mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
137-
#' mg + facet_grid(vs + am ~ gear)
138109
#' mg + facet_grid(vs + am ~ gear, margins = TRUE)
139110
#' mg + facet_grid(vs + am ~ gear, margins = "am")
140111
#' # when margins are made over "vs", since the facets for "am" vary
141112
#' # within the values of "vs", the marginal facet for "vs" is also
142113
#' # a margin over "am".
143114
#' mg + facet_grid(vs + am ~ gear, margins = "vs")
144-
#' mg + facet_grid(vs + am ~ gear, margins = "gear")
145-
#' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
146115
#' }
147116
facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
148117
space = "fixed", shrink = TRUE,

R/facet-wrap.r

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ NULL
4242
#' # You can facet by multiple variables
4343
#' ggplot(mpg, aes(displ, hwy)) +
4444
#' geom_point() +
45-
#' facet_wrap(~ cyl + drv)
46-
#' # Or use a character vector:
47-
#' ggplot(mpg, aes(displ, hwy)) +
48-
#' geom_point() +
49-
#' facet_wrap(c("cyl", "drv"))
45+
#' facet_wrap(vars(cyl, drv))
5046
#'
5147
#' # Use the `labeller` option to control how labels are printed:
5248
#' ggplot(mpg, aes(displ, hwy)) +

R/geom-point.r

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
#' p + geom_point(aes(shape = factor(cyl)))
5050
#' p + geom_point(aes(size = qsec))
5151
#'
52-
#' # Change scales
53-
#' p + geom_point(aes(colour = cyl)) + scale_colour_gradient(low = "blue")
54-
#' p + geom_point(aes(shape = factor(cyl))) + scale_shape(solid = FALSE)
55-
#'
5652
#' # Set aesthetics to fixed value
5753
#' ggplot(mtcars, aes(wt, mpg)) + geom_point(colour = "red", size = 3)
5854
#'

R/guide-legend.r

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,10 @@
6868
#' p2 <- p1 + geom_point(aes(size = value))
6969
#'
7070
#' # Basic form
71-
#' p1 + scale_fill_continuous(guide = "legend")
7271
#' p1 + scale_fill_continuous(guide = guide_legend())
7372
#'
74-
#' # Guide title
75-
#' p1 + scale_fill_continuous(guide = guide_legend(title = "V")) # title text
76-
#' p1 + scale_fill_continuous(guide = guide_legend(title = NULL)) # no title
77-
#'
7873
#' # Control styles
7974
#'
80-
#' # key size
81-
#' p1 + guides(fill = guide_legend(keywidth = 3, keyheight = 1))
82-
#'
8375
#' # title position
8476
#' p1 + guides(fill = guide_legend(title = "LEFT", title.position = "left"))
8577
#'
@@ -112,12 +104,10 @@
112104
#' )
113105
#'
114106
#' # Set aesthetic of legend key
115-
#'
116107
#' # very low alpha value make it difficult to see legend key
117-
#' p3 <- ggplot(diamonds, aes(carat, price)) +
118-
#' geom_point(aes(colour = color), alpha = 1/100)
108+
#' p3 <- ggplot(mtcars, aes(vs, am, colour = factor(cyl))) +
109+
#' geom_jitter(alpha = 1/5, width = 0.01, height = 0.01)
119110
#' p3
120-
#'
121111
#' # override.aes overwrites the alpha
122112
#' p3 + guides(colour = guide_legend(override.aes = list(alpha = 1)))
123113
#'
@@ -128,17 +118,9 @@
128118
#' p + guides(col = guide_legend(nrow = 8))
129119
#' p + guides(col = guide_legend(ncol = 8))
130120
#' p + guides(col = guide_legend(nrow = 8, byrow = TRUE))
131-
#' p + guides(col = guide_legend(ncol = 8, byrow = TRUE))
132121
#'
133122
#' # reversed order legend
134123
#' p + guides(col = guide_legend(reverse = TRUE))
135-
#'
136-
#' # hide some aesthetics from the legend
137-
#' p4 <- ggplot(mtcars, aes(mpg, qsec, colour = factor(vs), shape = factor(am))) +
138-
#' geom_point()
139-
#' p4 + geom_line()
140-
#' p4 + geom_line(show.legend = c(color = FALSE))
141-
#'
142124
#' }
143125
guide_legend <- function(# title
144126
title = waiver(),

R/labeller.r

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma"))
7070
#' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
7171
#'
72-
#' # Displaying only the values
73-
#' p + facet_grid(. ~ cyl)
72+
#' # The default is label_value
7473
#' p + facet_grid(. ~ cyl, labeller = label_value)
7574
#'
7675
#' \donttest{
@@ -84,7 +83,6 @@
8483
#' # Interpreting the labels as plotmath expressions
8584
#' p + facet_grid(. ~ cyl2)
8685
#' p + facet_grid(. ~ cyl2, labeller = label_parsed)
87-
#' p + facet_wrap(~vs + cyl2, labeller = label_parsed)
8886
#' }
8987
#' @name labellers
9088
NULL
@@ -364,12 +362,16 @@ as_labeller <- function(x, default = label_value, multi_line = TRUE) {
364362
#' p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
365363
#'
366364
#' # You can assign different labellers to variables:
367-
#' p1 + facet_grid(vs + am ~ gear,
368-
#' labeller = labeller(vs = label_both, am = label_value))
365+
#' p1 + facet_grid(
366+
#' vs + am ~ gear,
367+
#' labeller = labeller(vs = label_both, am = label_value)
368+
#' )
369369
#'
370370
#' # Or whole margins:
371-
#' p1 + facet_grid(vs + am ~ gear,
372-
#' labeller = labeller(.rows = label_both, .cols = label_value))
371+
#' p1 + facet_grid(
372+
#' vs + am ~ gear,
373+
#' labeller = labeller(.rows = label_both, .cols = label_value)
374+
#' )
373375
#'
374376
#' # You can supply functions operating on strings:
375377
#' capitalize <- function(string) {
@@ -399,9 +401,8 @@ as_labeller <- function(x, default = label_value, multi_line = TRUE) {
399401
#' # then apply a wrap labeller to the columns to prevent cropped text
400402
#' msleep$conservation2 <- plyr::revalue(msleep$conservation,
401403
#' conservation_status)
402-
#'
403-
#' p2 %+% msleep + facet_grid(vore ~ conservation2)
404-
#' p2 %+% msleep +
404+
#' p3 <- ggplot(msleep, aes(x = sleep_total, y = awake)) + geom_point()
405+
#' p3 +
405406
#' facet_grid(vore ~ conservation2,
406407
#' labeller = labeller(conservation2 = label_wrap_gen(10))
407408
#' )
@@ -418,8 +419,7 @@ as_labeller <- function(x, default = label_value, multi_line = TRUE) {
418419
#' )
419420
#'
420421
#' p2 + facet_grid(vore ~ conservation, labeller = global_labeller)
421-
#' p2 + facet_wrap(~vore, labeller = global_labeller)
422-
#' p2 %+% msleep + facet_wrap(~conservation2, labeller = global_labeller)
422+
#' p3 + facet_wrap(~conservation2, labeller = global_labeller)
423423
#' }
424424
labeller <- function(..., .rows = NULL, .cols = NULL,
425425
keep.as.numeric = NULL, .multi_line = TRUE,

man/facet_grid.Rd

Lines changed: 3 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/facet_wrap.Rd

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_point.Rd

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/guide_legend.Rd

Lines changed: 2 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)