Skip to content
Newer
Older
100644 416 lines (364 sloc) 15.3 KB
953fe47 @hadley Convert all documentation to roxygen
authored Dec 31, 2010
1 #' Lay out panels in a grid.
2 #'
3 #' @param facets a formula with the rows (of the tabular display) on the LHS
4 #' and the columns (of the tabular display) on the RHS; the dot in the
5 #' formula is used to indicate there should be no faceting on this dimension
6 #' (either row or column). The formula can also be provided as a string
7 #' instead of a classical formula object
61387cb @BrianDiggs Better documentation of margins argument to facet_grid (Issue #435)
BrianDiggs authored Apr 12, 2012
8 #' @param margins either a logical value or a character
9 #' vector. Margins are additional facets which contain all the data
10 #' for each of the possible values of the faceting variables. If
11 #' \code{FALSE}, no additional facets are included (the
12 #' default). If \code{TRUE}, margins are included for all faceting
13 #' variables. If specified as a character vector, it is the names of
14 #' variables for which margins are to be created.
9d3d8e8 @hadley Better documentation for facets
authored Dec 19, 2011
15 #' @param scales Are scales shared across all facets (the default,
16 #' \code{"fixed"}), or do they vary across rows (\code{"free_x"}),
17 #' columns (\code{"free_y"}), or both rows and columns (\code{"free"})
18 #' @param space If \code{"fixed"}, the default, all panels have the same size.
19 #' If \code{"free_y"} their height will be proportional to the length of the
1244716 @hadley Trim whitespace
authored Feb 24, 2014
20 #' y scale; if \code{"free_x"} their width will be proportional to the
21 #' length of the x scale; or if \code{"free"} both height and width will
9d3d8e8 @hadley Better documentation for facets
authored Dec 19, 2011
22 #' vary. This setting has no effect unless the appropriate scales also vary.
753f98c @lionel- New API for labellers
lionel- authored Jun 30, 2015
23 #' @param labeller A function that takes one data frame of labels and
24 #' returns a list or data frame of character vectors. Each input
25 #' column corresponds to one factor. Thus there will be more than
26 #' one with formulae of the type \code{~cyl + am}. Each output
27 #' column gets displayed as one separate line in the strip
28 #' label. This function should inherit from the "labeller" S3 class
29 #' for compatibility with \code{\link{labeller}()}. See
30 #' \code{\link{label_value}} for more details and pointers to other
31 #' options.
9d3d8e8 @hadley Better documentation for facets
authored Dec 19, 2011
32 #' @param as.table If \code{TRUE}, the default, the facets are laid out like
1244716 @hadley Trim whitespace
authored Feb 24, 2014
33 #' a table with highest values at the bottom-right. If \code{FALSE}, the
a43f684 @djmurphy420 Typo fixes in several help files.
djmurphy420 authored Dec 30, 2011
34 #' facets are laid out like a plot with the highest value at the top-right.
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
35 #' @param switch By default, the labels are displayed on the top and
36 #' right of the plot. If \code{"x"}, the top labels will be
37 #' displayed to the bottom. If \code{"y"}, the right-hand side
38 #' labels will be displayed to the left. Can also be set to
39 #' \code{"both"}.
9d3d8e8 @hadley Better documentation for facets
authored Dec 19, 2011
40 #' @param shrink If \code{TRUE}, will shrink scales to fit output of
41 #' statistics, not raw data. If \code{FALSE}, will be range of raw data
42 #' before statistical summary.
d72cc9b @hadley Support drop in facet_grid and facet_wrap.
authored Dec 23, 2011
43 #' @param drop If \code{TRUE}, the default, all factor levels not used in the
44 #' data will automatically be dropped. If \code{FALSE}, all factor levels
45 #' will be shown, regardless of whether or not they appear in the data.
953fe47 @hadley Convert all documentation to roxygen
authored Dec 31, 2010
46 #' @export
1244716 @hadley Trim whitespace
authored Feb 24, 2014
47 #' @examples
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
48 #' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
1244716 @hadley Trim whitespace
authored Feb 24, 2014
49 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
50 #' p + facet_grid(. ~ cyl)
51 #' p + facet_grid(drv ~ .)
52 #' p + facet_grid(drv ~ cyl)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
53 #'
83a2275 @djmurphy420 Faster example sets for selected geoms/stats
djmurphy420 authored Jan 14, 2012
54 #' # To change plot order of facet grid,
55 #' # change the order of variable levels with factor()
56 #'
2c88bd7 @hadley Minor fixes
authored Jun 12, 2015
57 #' # If you combine a facetted dataset with a dataset that lacks those
58 #' # facetting variables, the data will be repeated across the missing
59 #' # combinations:
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
60 #' df <- data.frame(displ = mean(mpg$displ), cty = mean(mpg$cty))
61 #' p +
62 #' facet_grid(. ~ cyl) +
a04742b @hrbrmstr Nearly done with qplot cleanup
hrbrmstr authored Jun 11, 2015
63 #' geom_point(data = df, colour = "red", size = 2)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
64 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
65 #' # Free scales -------------------------------------------------------
953fe47 @hadley Convert all documentation to roxygen
authored Dec 31, 2010
66 #' # You can also choose whether the scales should be constant
67 #' # across all panels (the default), or whether they should be allowed
68 #' # to vary
a04742b @hrbrmstr Nearly done with qplot cleanup
hrbrmstr authored Jun 11, 2015
69 #' mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
70 #' geom_point()
1244716 @hadley Trim whitespace
authored Feb 24, 2014
71 #'
953fe47 @hadley Convert all documentation to roxygen
authored Dec 31, 2010
72 #' mt + facet_grid(. ~ cyl, scales = "free")
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
73 #'
953fe47 @hadley Convert all documentation to roxygen
authored Dec 31, 2010
74 #' # If scales and space are free, then the mapping between position
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
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))
1244716 @hadley Trim whitespace
authored Feb 24, 2014
81 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
82 #' # Facet labels ------------------------------------------------------
83 #' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
84 #' p
1244716 @hadley Trim whitespace
authored Feb 24, 2014
85 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
86 #' # label_both() displays both variable name and value
87 #' p + facet_grid(vs ~ cyl, labeller = label_both)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
88 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
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)) +
a04742b @hrbrmstr Nearly done with qplot cleanup
hrbrmstr authored Jun 11, 2015
92 #' geom_point() +
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
93 #' facet_grid(. ~ cyl2, labeller = label_parsed)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
94 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
95 #' # label_bquote() makes it easy to construct math expressions
96 #' p + facet_grid(. ~ vs, labeller = label_bquote(cols = alpha ^ .(vs)))
1244716 @hadley Trim whitespace
authored Feb 24, 2014
97 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
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())
61387cb @BrianDiggs Better documentation of margins argument to facet_grid (Issue #435)
BrianDiggs authored Apr 12, 2012
108 #'
c68ce24 @hadley Speed up facet_grid examples
authored Jan 26, 2016
109 #' # Margins ----------------------------------------------------------
ab42c2c @hadley Improve dontrun usage
authored Feb 26, 2016
110 #' \donttest{
61387cb @BrianDiggs Better documentation of margins argument to facet_grid (Issue #435)
BrianDiggs authored Apr 12, 2012
111 #' # Margins can be specified by logically (all yes or all no) or by specific
112 #' # variables as (character) variable names
113 #' mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
114 #' mg + facet_grid(vs + am ~ gear)
115 #' mg + facet_grid(vs + am ~ gear, margins = TRUE)
116 #' mg + facet_grid(vs + am ~ gear, margins = "am")
117 #' # when margins are made over "vs", since the facets for "am" vary
118 #' # within the values of "vs", the marginal facet for "vs" is also
119 #' # a margin over "am".
120 #' mg + facet_grid(vs + am ~ gear, margins = "vs")
121 #' mg + facet_grid(vs + am ~ gear, margins = "gear")
122 #' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
c5b872e @hadley Don't test any example that takes over 5 seconds
authored Jan 25, 2012
123 #' }
4a8b435 @hadley Don't import all of plyr
authored Jul 24, 2015
124 #' @importFrom plyr as.quoted
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
125 facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE) {
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
126 scales <- match.arg(scales, c("fixed", "free_x", "free_y", "free"))
127 free <- list(
128 x = any(scales %in% c("free_x", "free")),
129 y = any(scales %in% c("free_y", "free"))
130 )
1244716 @hadley Trim whitespace
authored Feb 24, 2014
131
99b628a @wligtenberg Added free_x and free_y options to facet_grid space argument
wligtenberg authored Dec 23, 2011
132 space <- match.arg(space, c("fixed", "free_x", "free_y", "free"))
133 space_free <- list(
134 x = any(space %in% c("free_x", "free")),
135 y = any(space %in% c("free_y", "free"))
136 )
1244716 @hadley Trim whitespace
authored Feb 24, 2014
137
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
138 # Facets can either be a formula, a string, or a list of things to be
139 # convert to quoted
140 if (is.character(facets)) {
314b143 @hadley Be explicit about functions in base packages. #1197
authored Jul 24, 2015
141 facets <- stats::as.formula(facets)
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
142 }
143 if (is.formula(facets)) {
ec3cd49 @hadley Processing RStudio diagnostics
authored Jul 28, 2015
144 lhs <- function(x) if (length(x) == 2) NULL else x[-3]
145 rhs <- function(x) if (length(x) == 2) x else x[-2]
1244716 @hadley Trim whitespace
authored Feb 24, 2014
146
2ea092f @hadley Better parsing for facet_grid.
authored Feb 21, 2012
147 rows <- as.quoted(lhs(facets))
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
148 rows <- rows[!sapply(rows, identical, as.name("."))]
2ea092f @hadley Better parsing for facet_grid.
authored Feb 21, 2012
149 cols <- as.quoted(rhs(facets))
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
150 cols <- cols[!sapply(cols, identical, as.name("."))]
151 }
152 if (is.list(facets)) {
153 rows <- as.quoted(facets[[1]])
154 cols <- as.quoted(facets[[2]])
155 }
156 if (length(rows) + length(cols) == 0) {
157 stop("Must specify at least one variable to facet by", call. = FALSE)
6198457 @hadley Add ggplot
authored Nov 6, 2007
158 }
1244716 @hadley Trim whitespace
authored Feb 24, 2014
159
1fba535 @lionel- Handle deprecated labellers
lionel- authored Oct 16, 2015
160 # Check for deprecated labellers
161 labeller <- check_labeller(labeller)
162
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
163 facet(
164 rows = rows, cols = cols, margins = margins, shrink = shrink,
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
165 free = free, space_free = space_free, labeller = labeller,
166 as.table = as.table, switch = switch, drop = drop,
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
167 subclass = "grid"
168 )
169 }
b19ff3f @hadley Port over the last changes. Now to get it working
authored May 18, 2011
170
2ea092f @hadley Better parsing for facet_grid.
authored Feb 21, 2012
171
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
172 #' @export
1244716 @hadley Trim whitespace
authored Feb 24, 2014
173 facet_train_layout.grid <- function(facet, data) {
d72cc9b @hadley Support drop in facet_grid and facet_wrap.
authored Dec 23, 2011
174 layout <- layout_grid(data, facet$rows, facet$cols, facet$margins,
aaa334d @hadley Implement as.table for facet_grid.
authored Apr 20, 2012
175 drop = facet$drop, as.table = facet$as.table)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
176
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
177 # Relax constraints, if necessary
d72cc9b @hadley Support drop in facet_grid and facet_wrap.
authored Dec 23, 2011
178 layout$SCALE_X <- if (facet$free$x) layout$COL else 1L
179 layout$SCALE_Y <- if (facet$free$y) layout$ROW else 1L
1244716 @hadley Trim whitespace
authored Feb 24, 2014
180
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
181 layout
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
182 }
cd11888 @hadley Fix aspect ratio in facet_wrap. First pass at improved coord_equal - …
authored Dec 9, 2009
183
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
184
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
185 #' @export
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
186 facet_map_layout.grid <- function(facet, data, layout) {
187 locate_grid(data, layout, facet$rows, facet$cols, facet$margins)
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
188 }
189
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
190 #' @export
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
191 facet_render.grid <- function(facet, panel, coord, theme, geom_grobs) {
192 axes <- facet_axes(facet, panel, coord, theme)
193 strips <- facet_strips(facet, panel, theme)
194 panels <- facet_panels(facet, panel, coord, theme, geom_grobs)
aa7c357 @kohske fix a bug with facet_grid aspect ratio and add test codes
kohske authored Aug 16, 2011
195
196 # adjust the size of axes to the size of panel
197 axes$l$heights <- panels$heights
198 axes$b$widths <- panels$widths
1244716 @hadley Trim whitespace
authored Feb 24, 2014
199
faded39 @hadley Fix panel sizing. Fixes #312
authored Dec 21, 2011
200 # adjust the size of the strips to the size of the panels
201 strips$r$heights <- panels$heights
202 strips$t$widths <- panels$widths
1244716 @hadley Trim whitespace
authored Feb 24, 2014
203
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
204 # Check if switch is consistent with grid layout
205 switch_x <- !is.null(facet$switch) && facet$switch %in% c("both", "x")
206 switch_y <- !is.null(facet$switch) && facet$switch %in% c("both", "y")
207 if (switch_x && length(strips$t) == 0) {
208 facet$switch <- if (facet$switch == "both") "y" else NULL
209 switch_x <- FALSE
210 warning("Cannot switch x axis strips as they do not exist", call. = FALSE)
211 }
212 if (switch_y && length(strips$r) == 0) {
213 facet$switch <- if (facet$switch == "both") "x" else NULL
214 switch_y <- FALSE
215 warning("Cannot switch y axis strips as they do not exist", call. = FALSE)
216 }
217
218
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
219 # Combine components into complete plot
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
220 if (is.null(facet$switch)) {
221 top <- strips$t
222 top <- gtable_add_cols(top, strips$r$widths)
223 top <- gtable_add_cols(top, axes$l$widths, pos = 0)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
224
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
225 center <- cbind(axes$l, panels, strips$r, z = c(2, 1, 3))
226 bottom <- axes$b
227 bottom <- gtable_add_cols(bottom, strips$r$widths)
228 bottom <- gtable_add_cols(bottom, axes$l$widths, pos = 0)
229
230 complete <- rbind(top, center, bottom, z = c(1, 2, 3))
231
232 } else {
233 # Add padding between the switched strips and the axes
234 padding <- convertUnit(theme$strip.switch.pad.grid, "cm")
4a8b435 @hadley Don't import all of plyr
authored Jul 24, 2015
235
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
236 if (switch_x) {
237 t_heights <- c(padding, strips$t$heights)
238 gt_t <- gtable(widths = strips$t$widths, heights = unit(t_heights, "cm"))
239 gt_t <- gtable_add_grob(gt_t, strips$t, name = strips$t$name, clip = "off",
240 t = 1, l = 1, b = -1, r = -1)
241 }
242 if (switch_y) {
243 r_widths <- c(strips$r$widths, padding)
244 gt_r <- gtable(widths = unit(r_widths, "cm"), heights = strips$r$heights)
245 gt_r <- gtable_add_grob(gt_r, strips$r, name = strips$r$name, clip = "off",
246 t = 1, l = 1, b = -1, r = -1)
247 }
248
249 # Combine plot elements according to strip positions
250 if (switch_x && switch_y) {
251 center <- cbind(gt_r, axes$l, panels, z = c(3, 2, 1))
252
253 bottom <- rbind(axes$b, gt_t)
254 bottom <- gtable_add_cols(bottom, axes$l$widths, pos = 0)
255 bottom <- gtable_add_cols(bottom, gt_r$widths, pos = 0)
256
257 complete <- rbind(center, bottom, z = c(1, 2))
258 } else if (switch_x) {
259 center <- cbind(axes$l, panels, strips$r, z = c(2, 1, 3))
6f52958 @hadley Rewrite TableLayout in S3.
authored May 21, 2011
260
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
261 bottom <- rbind(axes$b, gt_t)
262 bottom <- gtable_add_cols(bottom, strips$r$widths)
263 bottom <- gtable_add_cols(bottom, axes$l$widths, pos = 0)
264
265 complete <- rbind(center, bottom, z = c(1, 2))
266 } else if (switch_y) {
267 top <- strips$t
268 top <- gtable_add_cols(top, axes$l$widths, pos = 0)
ebc8a1c @lionel- Fix padding issue with switched facet strips
lionel- authored Jul 28, 2015
269 top <- gtable_add_cols(top, gt_r$widths, pos = 0)
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
270
271 center <- cbind(gt_r, axes$l, panels, z = c(3, 2, 1))
272 bottom <- axes$b
273 bottom <- gtable_add_cols(bottom, axes$l$widths, pos = 0)
274 bottom <- gtable_add_cols(bottom, gt_r$widths, pos = 0)
275
276 complete <- rbind(top, center, bottom, z = c(1, 2, 3))
277 } else {
278 stop("`switch` must be either NULL, 'both', 'x', or 'y'",
4a8b435 @hadley Don't import all of plyr
authored Jul 24, 2015
279 call. = FALSE)
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
280 }
281 }
4a8b435 @hadley Don't import all of plyr
authored Jul 24, 2015
282
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
283 complete$respect <- panels$respect
284 complete$name <- "layout"
bd8830c @wch Put axis lines above panel for facet_grid
wch authored Jul 3, 2012
285 bottom <- axes$b
1244716 @hadley Trim whitespace
authored Feb 24, 2014
286
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
287 complete
288 }
26411d4 @hadley Use new layout method for facet_grid
authored Oct 15, 2008
289
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
290 #' @export
bbff5fb @hadley Layout for facet_wrap
authored May 20, 2011
291 facet_strips.grid <- function(facet, panel, theme) {
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
292 col_vars <- unique(panel$layout[names(facet$cols)])
293 row_vars <- unique(panel$layout[names(facet$rows)])
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
294
114894f @lionel- Add metadata information on the facet labels
lionel- authored Sep 1, 2015
295 # Adding labels metadata, useful for labellers
296 attr(col_vars, "type") <- "cols"
297 attr(col_vars, "facet") <- "grid"
298 attr(row_vars, "type") <- "rows"
299 attr(row_vars, "facet") <- "grid"
300
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
301 dir <- list(r = "r", t = "t")
302 if (!is.null(facet$switch) && facet$switch %in% c("both", "x")) {
303 dir$t <- "b"
304 }
ec3cd49 @hadley Processing RStudio diagnostics
authored Jul 28, 2015
305 if (!is.null(facet$switch) && facet$switch %in% c("both", "y")) {
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
306 dir$r <- "l"
307 }
308
753f98c @lionel- New API for labellers
lionel- authored Jun 30, 2015
309 strips <- list(
0ad37dd @lionel- Add `switch` option to facet_grid() and facet_wrap()
lionel- authored Jun 9, 2014
310 r = build_strip(panel, row_vars, facet$labeller,
311 theme, dir$r, switch = facet$switch),
312 t = build_strip(panel, col_vars, facet$labeller,
313 theme, dir$t, switch = facet$switch)
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
314 )
1244716 @hadley Trim whitespace
authored Feb 24, 2014
315
753f98c @lionel- New API for labellers
lionel- authored Jun 30, 2015
316 Map(function(strip, side) {
317 if (side %in% c("t", "b")) {
318 gtable_add_col_space(strip, theme$panel.margin.x %||% theme$panel.margin)
26411d4 @hadley Use new layout method for facet_grid
authored Oct 15, 2008
319 } else {
753f98c @lionel- New API for labellers
lionel- authored Jun 30, 2015
320 gtable_add_row_space(strip, theme$panel.margin.y %||% theme$panel.margin)
26411d4 @hadley Use new layout method for facet_grid
authored Oct 15, 2008
321 }
753f98c @lionel- New API for labellers
lionel- authored Jun 30, 2015
322 }, strips, dir)
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
323 }
b19ff3f @hadley Port over the last changes. Now to get it working
authored May 18, 2011
324
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
325 #' @export
bbff5fb @hadley Layout for facet_wrap
authored May 20, 2011
326 facet_axes.grid <- function(facet, panel, coord, theme) {
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
327 axes <- list()
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
328
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
329 # Horizontal axes
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
330 cols <- which(panel$layout$ROW == 1)
1a4e6b6 @wch Fix remaining calls to coord_ functions
wch authored Jul 24, 2015
331 grobs <- lapply(panel$ranges[cols], coord$render_axis_h, theme = theme)
c7cb1bf @wch Remove utilities-table-template.r and migrate to gtable package
wch authored Jun 26, 2012
332 axes$b <- gtable_add_col_space(gtable_row("axis-b", grobs),
9df62a6 @krlmlr New themes panel.margin.x and panel.margin.y
krlmlr authored Jul 11, 2013
333 theme$panel.margin.x %||% theme$panel.margin)
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
334
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
335 # Vertical axes
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
336 rows <- which(panel$layout$COL == 1)
1a4e6b6 @wch Fix remaining calls to coord_ functions
wch authored Jul 24, 2015
337 grobs <- lapply(panel$ranges[rows], coord$render_axis_v, theme = theme)
c7cb1bf @wch Remove utilities-table-template.r and migrate to gtable package
wch authored Jun 27, 2012
338 axes$l <- gtable_add_row_space(gtable_col("axis-l", grobs),
9df62a6 @krlmlr New themes panel.margin.x and panel.margin.y
krlmlr authored Jul 11, 2013
339 theme$panel.margin.y %||% theme$panel.margin)
1ba0154 @hadley Pass correct position scales to statistics
authored Nov 10, 2008
340
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
341 axes
342 }
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
343
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
344 #' @export
bbff5fb @hadley Layout for facet_wrap
authored May 20, 2011
345 facet_panels.grid <- function(facet, panel, coord, theme, geom_grobs) {
1244716 @hadley Trim whitespace
authored Feb 24, 2014
346
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
347 # If user hasn't set aspect ratio, and we have fixed scales, then
348 # ask the coordinate system if it wants to specify one
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
349 aspect_ratio <- theme$aspect.ratio
350 if (is.null(aspect_ratio) && !facet$free$x && !facet$free$y) {
1a4e6b6 @wch Fix remaining calls to coord_ functions
wch authored Jul 24, 2015
351 aspect_ratio <- coord$aspect(panel$ranges[[1]])
1ba0154 @hadley Pass correct position scales to statistics
authored Nov 10, 2008
352 }
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
353 if (is.null(aspect_ratio)) {
354 aspect_ratio <- 1
355 respect <- FALSE
356 } else {
357 respect <- TRUE
ff99338 @hadley Improve summary output
authored Jun 17, 2008
358 }
1244716 @hadley Trim whitespace
authored Feb 24, 2014
359
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
360 # Add background and foreground to panels
1244716 @hadley Trim whitespace
authored Feb 24, 2014
361 panels <- panel$layout$PANEL
c093bc8 @hadley Single row/col facetting working
authored May 19, 2011
362 ncol <- max(panel$layout$COL)
363 nrow <- max(panel$layout$ROW)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
364
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
365 panel_grobs <- lapply(panels, function(i) {
1a4e6b6 @wch Fix remaining calls to coord_ functions
wch authored Jul 24, 2015
366 fg <- coord$render_fg(panel$ranges[[i]], theme)
367 bg <- coord$render_bg(panel$ranges[[i]], theme)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
368
3713910 @hadley Refactoring Geom. Fixes #1258
authored Aug 25, 2015
369 geom_grobs <- lapply(geom_grobs, `[[`, i)
2e722cc @noamross New theme option to put panel/grid on top of data
noamross authored Aug 5, 2014
370
ec3cd49 @hadley Processing RStudio diagnostics
authored Jul 28, 2015
371 if (theme$panel.ontop) {
2e722cc @noamross New theme option to put panel/grid on top of data
noamross authored Aug 6, 2014
372 panel_grobs <- c(geom_grobs, list(bg), list(fg))
373 } else {
374 panel_grobs <- c(list(bg), geom_grobs, list(fg))
375 }
1244716 @hadley Trim whitespace
authored Feb 24, 2014
376
377 gTree(children = do.call("gList", panel_grobs))
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
378 })
1244716 @hadley Trim whitespace
authored Feb 24, 2014
379
16c0e55 @hadley More example fixes
authored Dec 29, 2011
380 panel_matrix <- matrix(panel_grobs, nrow = nrow, ncol = ncol, byrow = TRUE)
629c1f2 @kohske moves expand calculation in coord_train.
kohske authored Apr 14, 2012
381
382 # @kohske
383 # Now size of each panel is calculated using PANEL$ranges, which is given by
384 # coord_train called by train_range.
385 # So here, "scale" need not to be referred.
386 #
387 # In general, panel has all information for building facet.
99b628a @wligtenberg Added free_x and free_y options to facet_grid space argument
wligtenberg authored Dec 23, 2011
388 if (facet$space_free$x) {
9bf5228 @kohske tweak a code in facet-grid.
kohske authored Apr 17, 2012
389 ps <- panel$layout$PANEL[panel$layout$ROW == 1]
9d2cddc @jimhester Fix all found instances of partial matching
jimhester authored Jun 16, 2015
390 widths <- vapply(ps, function(i) diff(panel$ranges[[i]]$x.range), numeric(1))
9bf5228 @kohske tweak a code in facet-grid.
kohske authored Apr 17, 2012
391 panel_widths <- unit(widths, "null")
c338a52 @wligtenberg Latest comments incorporated
wligtenberg authored Dec 23, 2011
392 } else {
393 panel_widths <- rep(unit(1, "null"), ncol)
99b628a @wligtenberg Added free_x and free_y options to facet_grid space argument
wligtenberg authored Dec 23, 2011
394 }
395 if (facet$space_free$y) {
9bf5228 @kohske tweak a code in facet-grid.
kohske authored Apr 17, 2012
396 ps <- panel$layout$PANEL[panel$layout$COL == 1]
9d2cddc @jimhester Fix all found instances of partial matching
jimhester authored Jun 16, 2015
397 heights <- vapply(ps, function(i) diff(panel$ranges[[i]]$y.range), numeric(1))
9bf5228 @kohske tweak a code in facet-grid.
kohske authored Apr 17, 2012
398 panel_heights <- unit(heights, "null")
c338a52 @wligtenberg Latest comments incorporated
wligtenberg authored Dec 23, 2011
399 } else {
400 panel_heights <- rep(unit(1 * aspect_ratio, "null"), nrow)
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
401 }
1244716 @hadley Trim whitespace
authored Feb 24, 2014
402
c7cb1bf @wch Remove utilities-table-template.r and migrate to gtable package
wch authored Jun 27, 2012
403 panels <- gtable_matrix("panel", panel_matrix,
6f52958 @hadley Rewrite TableLayout in S3.
authored May 21, 2011
404 panel_widths, panel_heights, respect = respect)
9df62a6 @krlmlr New themes panel.margin.x and panel.margin.y
krlmlr authored Jul 11, 2013
405 panels <- gtable_add_col_space(panels, theme$panel.margin.x %||% theme$panel.margin)
406 panels <- gtable_add_row_space(panels, theme$panel.margin.y %||% theme$panel.margin)
1244716 @hadley Trim whitespace
authored Feb 24, 2014
407
a7f3c10 @hadley Convert facets to S3
authored May 19, 2011
408 panels
409 }
410
3c63ff7 @hadley Convert from @S3method to @export
authored Feb 24, 2014
411 #' @export
c6c9a2c @hadley Add facet print method so summary works.
authored Dec 10, 2011
412 facet_vars.grid <- function(facet) {
1244716 @hadley Trim whitespace
authored Feb 24, 2014
413 paste(lapply(list(facet$rows, facet$cols), paste, collapse = ", "),
c6c9a2c @hadley Add facet print method so summary works.
authored Dec 10, 2011
414 collapse = " ~ ")
415 }
Something went wrong with that request. Please try again.