Skip to content

Commit c68ce24

Browse files
committed
Speed up facet_grid examples
1 parent 977312d commit c68ce24

File tree

2 files changed

+79
-207
lines changed

2 files changed

+79
-207
lines changed

R/facet-grid-.r

Lines changed: 38 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -45,119 +45,69 @@
4545
#' will be shown, regardless of whether or not they appear in the data.
4646
#' @export
4747
#' @examples
48-
#' \donttest{
49-
#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
50-
#' # With one variable
51-
#' p + facet_grid(. ~ cyl)
52-
#' p + facet_grid(cyl ~ .)
48+
#' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
5349
#'
54-
#' # With two variables
55-
#' p + facet_grid(vs ~ am)
56-
#' p + facet_grid(am ~ vs)
57-
#' p + facet_grid(vs ~ am, margins=TRUE)
50+
#' p + facet_grid(. ~ cyl)
51+
#' p + facet_grid(drv ~ .)
52+
#' p + facet_grid(drv ~ cyl)
5853
#'
5954
#' # To change plot order of facet grid,
6055
#' # change the order of variable levels with factor()
6156
#'
62-
#' set.seed(6809)
63-
#' diamonds <- diamonds[sample(nrow(diamonds), 1000), ]
64-
#' diamonds$cut <- factor(diamonds$cut,
65-
#' levels = c("Ideal", "Very Good", "Fair", "Good", "Premium"))
66-
#'
67-
#' # Repeat first example with new order
68-
#' p <- ggplot(diamonds, aes(carat, ..density..)) +
69-
#' geom_histogram(binwidth = 1)
70-
#' p + facet_grid(. ~ cut)
71-
#'
72-
#' g <- ggplot(mtcars, aes(mpg, wt)) +
73-
#' geom_point()
74-
#' g + facet_grid(. ~ vs + am)
75-
#' g + facet_grid(vs + am ~ .)
76-
#'
77-
#' # You can also use strings, which makes it a little easier
78-
#' # when writing functions that generate faceting specifications
79-
#'
80-
#' p + facet_grid("cut ~ .")
81-
#'
82-
#' # see also ?plotmatrix for the scatterplot matrix
83-
#'
84-
#' # If there isn't any data for a given combination, that panel
85-
#' # will be empty
86-
#'
87-
#' g + facet_grid(cyl ~ vs)
88-
#'
8957
#' # If you combine a facetted dataset with a dataset that lacks those
9058
#' # facetting variables, the data will be repeated across the missing
9159
#' # combinations:
92-
#'
93-
#' g + facet_grid(vs ~ cyl)
94-
#'
95-
#' df <- data.frame(mpg = 22, wt = 3)
96-
#' g + facet_grid(vs ~ cyl) +
60+
#' df <- data.frame(displ = mean(mpg$displ), cty = mean(mpg$cty))
61+
#' p +
62+
#' facet_grid(. ~ cyl) +
9763
#' geom_point(data = df, colour = "red", size = 2)
9864
#'
99-
#' df2 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(0, 1))
100-
#' g + facet_grid(vs ~ cyl) +
101-
#' geom_point(data = df2, colour = "red", size = 2)
102-
#'
103-
#' df3 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(1, 1))
104-
#' g + facet_grid(vs ~ cyl) +
105-
#' geom_point(data = df3, colour = "red", size = 2)
106-
#'
107-
#'
65+
#' # Free scales -------------------------------------------------------
10866
#' # You can also choose whether the scales should be constant
10967
#' # across all panels (the default), or whether they should be allowed
11068
#' # to vary
11169
#' mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
11270
#' geom_point()
11371
#'
11472
#' mt + facet_grid(. ~ cyl, scales = "free")
73+
#'
11574
#' # If scales and space are free, then the mapping between position
116-
#' # and values in the data will be the same across all panels
117-
#' mt + facet_grid(. ~ cyl, scales = "free", space = "free")
75+
#' # and values in the data will be the same across all panels. This
76+
#' # is particularly useful for categorical axes
77+
#' ggplot(mpg, aes(drv, model)) +
78+
#' geom_point() +
79+
#' facet_grid(manufacturer ~ ., scales = "free", space = "free") +
80+
#' theme(strip.text.y = element_text(angle = 0))
11881
#'
119-
#' mt + facet_grid(vs ~ am, scales = "free")
120-
#' mt + facet_grid(vs ~ am, scales = "free_x")
121-
#' mt + facet_grid(vs ~ am, scales = "free_y")
122-
#' mt + facet_grid(vs ~ am, scales = "free", space = "free")
123-
#' mt + facet_grid(vs ~ am, scales = "free", space = "free_x")
124-
#' mt + facet_grid(vs ~ am, scales = "free", space = "free_y")
82+
#' # Facet labels ------------------------------------------------------
83+
#' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
84+
#' p
12585
#'
126-
#' # You may need to set your own breaks for consistent display:
127-
#' mt + facet_grid(. ~ cyl, scales = "free_x", space = "free") +
128-
#' scale_x_continuous(breaks = seq(10, 36, by = 2))
129-
#' # Adding scale limits override free scales:
130-
#' last_plot() + xlim(10, 15)
86+
#' # label_both() displays both variable name and value
87+
#' p + facet_grid(vs ~ cyl, labeller = label_both)
13188
#'
132-
#' # Free scales are particularly useful for categorical variables
133-
#' ggplot(mpg, aes(cty, model)) +
89+
#' # label_parsed() parses text into mathematical expressions, see ?plotmath
90+
#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)"))
91+
#' ggplot(mtcars, aes(wt, mpg)) +
13492
#' geom_point() +
135-
#' facet_grid(manufacturer ~ ., scales = "free", space = "free")
136-
#' # particularly when you reorder factor levels
137-
#' mpg$model <- reorder(mpg$model, mpg$cty)
138-
#' manufacturer <- reorder(mpg$manufacturer, mpg$cty)
139-
#' last_plot() %+% mpg + theme(strip.text.y = element_text())
93+
#' facet_grid(. ~ cyl2, labeller = label_parsed)
14094
#'
141-
#' # Use as.table to to control direction of horizontal facets, TRUE by default
142-
#' h <- ggplot(mtcars, aes(x = mpg, y = wt)) +
143-
#' geom_point()
144-
#' h + facet_grid(cyl ~ vs)
145-
#' h + facet_grid(cyl ~ vs, as.table = FALSE)
95+
#' # label_bquote() makes it easy to construct math expressions
96+
#' p + facet_grid(. ~ vs, labeller = label_bquote(cols = alpha ^ .(vs)))
14697
#'
147-
#' # Use labeller to control facet labels, label_value is default
148-
#' h + facet_grid(cyl ~ vs, labeller = label_both)
149-
#' # Using label_parsed, see ?plotmath for more options
150-
#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)"))
151-
#' k <- ggplot(mtcars, aes(wt, mpg)) +
152-
#' geom_point()
153-
#' k + facet_grid(. ~ cyl2)
154-
#' k + facet_grid(. ~ cyl2, labeller = label_parsed)
155-
#' # For label_bquote the label value is x.
156-
#' p <- ggplot(mtcars, aes(wt, mpg)) +
157-
#' geom_point()
158-
#' p + facet_grid(. ~ vs, labeller = label_bquote(alpha ^ .(x)))
159-
#' p + facet_grid(. ~ vs, labeller = label_bquote(.(x) ^ .(x)))
98+
#' # The facet strips can be displayed near the axes with switch
99+
#' data <- transform(mtcars,
100+
#' am = factor(am, levels = 0:1, c("Automatic", "Manual")),
101+
#' gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five"))
102+
#' )
103+
#' p <- ggplot(data, aes(mpg, disp)) + geom_point()
104+
#' p + facet_grid(am ~ gear, switch = "both")
105+
#' # It looks better without boxes around the strips
106+
#' p + facet_grid(am ~ gear, switch = "both") +
107+
#' theme(strip.background = element_blank())
160108
#'
109+
#' # Margins ----------------------------------------------------------
110+
#' \dontrun{
161111
#' # Margins can be specified by logically (all yes or all no) or by specific
162112
#' # variables as (character) variable names
163113
#' mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
@@ -170,20 +120,6 @@
170120
#' mg + facet_grid(vs + am ~ gear, margins = "vs")
171121
#' mg + facet_grid(vs + am ~ gear, margins = "gear")
172122
#' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
173-
#'
174-
#' # The facet strips can be displayed near the axes with switch
175-
#' data <- transform(mtcars,
176-
#' am = factor(am, levels = 0:1, c("Automatic", "Manual")),
177-
#' gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five"))
178-
#' )
179-
#' p <- ggplot(data, aes(mpg, disp)) + geom_point()
180-
#' p + facet_grid(am ~ gear, switch = "both") + theme_light()
181-
#'
182-
#' # It may be more aesthetic to use a theme without boxes around
183-
#' # around the strips.
184-
#' p + facet_grid(am ~ gear + vs, switch = "y") + theme_minimal()
185-
#' p + facet_grid(am ~ ., switch = "y") +
186-
#' theme_gray() %+replace% theme(strip.background = element_blank())
187123
#' }
188124
#' @importFrom plyr as.quoted
189125
facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE) {

man/facet_grid.Rd

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

0 commit comments

Comments
 (0)