7
7
# ' faceting variables. It is most useful when you have two discrete
8
8
# ' variables, and all combinations of the variables exist in the data.
9
9
# '
10
- # ' @param facets a formula with the rows ( of the tabular display) on the LHS
11
- # ' and the columns (of the tabular display) on the RHS; the dot in the
12
- # ' formula is used to indicate there should be no faceting on this dimension
13
- # ' (either row or column). The formula can also be provided as a string
14
- # ' instead of a classical formula object
15
- # ' @param margins either a logical value or a character
16
- # ' vector. Margins are additional facets which contain all the data
17
- # ' for each of the possible values of the faceting variables. If
18
- # ' `FALSE`, no additional facets are included (the
19
- # ' default). If `TRUE`, margins are included for all faceting
20
- # ' variables. If specified as a character vector, it is the names of
21
- # ' variables for which margins are to be created .
10
+ # ' @param rows,cols A set of variables or expressions quoted by
11
+ # ' [vars()] and defining faceting groups on the rows or columns
12
+ # ' dimension. The names will be shown if a relevant `labeller` is
13
+ # ' set.
14
+ # '
15
+ # ' For backward compatibility with the historical interface, `rows`
16
+ # ' can also be a formula with the rows (of the tabular display) on
17
+ # ' the LHS and the columns ( of the tabular display) on the RHS; the
18
+ # ' dot in the formula is used to indicate there should be no
19
+ # ' faceting on this dimension (either row or column). The formula
20
+ # ' can also be provided as a string instead of a classical formula
21
+ # ' object .
22
22
# ' @param scales Are scales shared across all facets (the default,
23
23
# ' `"fixed"`), or do they vary across rows (`"free_x"`),
24
24
# ' columns (`"free_y"`), or both rows and columns (`"free"`)
50
50
# ' @param drop If `TRUE`, the default, all factor levels not used in the
51
51
# ' data will automatically be dropped. If `FALSE`, all factor levels
52
52
# ' will be shown, regardless of whether or not they appear in the data.
53
+ # ' @param margins either a logical value or a character
54
+ # ' vector. Margins are additional facets which contain all the data
55
+ # ' for each of the possible values of the faceting variables. If
56
+ # ' `FALSE`, no additional facets are included (the
57
+ # ' default). If `TRUE`, margins are included for all faceting
58
+ # ' variables. If specified as a character vector, it is the names of
59
+ # ' variables for which margins are to be created.
60
+ # ' @param facets This argument is soft-deprecated, please us `rows`
61
+ # ' and `cols` instead.
53
62
# ' @export
54
63
# ' @examples
55
64
# ' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
@@ -129,7 +138,27 @@ NULL
129
138
# ' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
130
139
# ' }
131
140
# ' @importFrom plyr as.quoted
132
- facet_grid <- function (facets , margins = FALSE , scales = " fixed" , space = " fixed" , shrink = TRUE , labeller = " label_value" , as.table = TRUE , switch = NULL , drop = TRUE ) {
141
+ facet_grid <- function (rows = NULL ,
142
+ cols = NULL ,
143
+ scales = " fixed" ,
144
+ space = " fixed" ,
145
+ shrink = TRUE ,
146
+ labeller = " label_value" ,
147
+ as.table = TRUE ,
148
+ switch = NULL ,
149
+ drop = TRUE ,
150
+ margins = FALSE ,
151
+ facets = NULL ) {
152
+ # `facets` is soft-deprecated and renamed to `rows`
153
+ if (! is.null(facets )) {
154
+ rows <- facets
155
+ }
156
+ # Should become a warning in a future release
157
+ if (is.logical(cols )) {
158
+ margins <- cols
159
+ cols <- NULL
160
+ }
161
+
133
162
scales <- match.arg(scales , c(" fixed" , " free_x" , " free_y" , " free" ))
134
163
free <- list (
135
164
x = any(scales %in% c(" free_x" , " free" )),
@@ -146,21 +175,17 @@ facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed
146
175
stop(" switch must be either 'both', 'x', or 'y'" , call. = FALSE )
147
176
}
148
177
149
- facets <- as_facets_list(facets )
150
-
151
- n <- length(facets )
152
- if (! n ) {
153
- stop(" FIXME: Internal error grid?" )
154
- }
178
+ facets_list <- grid_as_facets_list(rows , cols )
179
+ n <- length(facets_list )
155
180
if (n > 2L ) {
156
181
stop(" A grid facet specification can't have more than two dimensions" , call. = FALSE )
157
182
}
158
183
if (n == 1L ) {
159
- rows <- list ()
160
- cols <- facets [[1 ]]
184
+ rows <- quos ()
185
+ cols <- facets_list [[1 ]]
161
186
} else {
162
- rows <- facets [[1 ]]
163
- cols <- facets [[2 ]]
187
+ rows <- facets_list [[1 ]]
188
+ cols <- facets_list [[2 ]]
164
189
}
165
190
166
191
# Check for deprecated labellers
@@ -173,6 +198,33 @@ facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed
173
198
as.table = as.table , switch = switch , drop = drop )
174
199
)
175
200
}
201
+ grid_as_facets_list <- function (rows , cols ) {
202
+ is_rows_vars <- is.null(rows ) || rlang :: is_quosures(rows )
203
+ if (! is_rows_vars ) {
204
+ if (! is.null(cols )) {
205
+ stop(" `rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list" , call. = FALSE )
206
+ }
207
+ return (as_facets_list(rows ))
208
+ }
209
+
210
+ is_cols_vars <- is.null(cols ) || rlang :: is_quosures(cols )
211
+ if (! is_cols_vars ) {
212
+ stop(" `cols` must be `NULL` or a `vars()` specification" , call. = FALSE )
213
+ }
214
+
215
+ if (is.null(rows )) {
216
+ rows <- quos()
217
+ } else {
218
+ rows <- rlang :: quos_auto_name(rows )
219
+ }
220
+ if (is.null(cols )) {
221
+ cols <- quos()
222
+ } else {
223
+ cols <- rlang :: quos_auto_name(cols )
224
+ }
225
+
226
+ list (rows , cols )
227
+ }
176
228
177
229
# ' @rdname ggplot2-ggproto
178
230
# ' @format NULL
0 commit comments