All facet_*() functions returns a Facet object or an object of a
Facet subclass. This object describes how to assign data to different
panels, how to apply positional scales and how to lay out the panels, once
rendered.
Details
Extending facets can range from the simple modifications of current facets,
to very laborious rewrites with a lot of gtable()
manipulation.For some examples of both, please see the extension vignette.
The object and its parameters are chaperoned by the Layout class.
Facet subclasses, like other extendible ggproto classes, have a range
of methods that can be modified. Some of these are required for all new
subclasses, while other only need to be modified if need arises.
The required methods are:
compute_layoutdraw_panels()or its subsidiaries:init_gtable()attach_axes()attach_strips()
In addition to the methods above, it can be useful to override the default behaviour of one or more of the following methods:
setup_params()init_scales()train_scale()finish_data()draw_back(),draw_front()ordraw_labels()
All extension methods receive the content of the params field as the params argument, so the constructor function will generally put all relevant information into this field.
Fields
shinkA scalar boolean which when
TRUE, will shrink scales to fit output statistics rather than raw data. IfFALSE, will only include raw data before statistical summary. By exception this is not part of theparamsfield.paramsA named list of parameters populated by the constructor function.
setup_paramsDescription
A function method for modifying or checking the parameters based on the data. The default method includes a
.possible_columnsvariable giving column names.Usage
Facet$setup_params(data, params)Arguments
dataA list of data frames. The first item is the global data, which is followed by layer data in subsequent items.
paramsA list of current parameters.
Value
A list of parameters
setup_dataDescription
A function method for modifying or checking the data prior to adding defaults. The default method returns data unaltered.
Usage
Facet$setup_data(data, params)Arguments
dataA list of data frames. The first item is the global data, which is followed by layer data in subsequent items.
paramsA list of parameters coming from the
setup_params()method.
Value
A list of data frames of the same length as the
dataargumentcompute_layoutDescription
A function method for creating the correspondence between faceting variable levels, panels and position scales. It places panels like cells in a matrix.
Usage
Facet$compute_layout(data, params)Arguments
dataA list of data frames. The first item is the global data, which is followed by layer data in subsequent items.
paramsA list of parameters coming from the
setup_params()method.
Value
A data frame with 1 row per panel, containing at least integer columns
ROW,COL,PANEL,SCALE_XandSCALE_Y. Can contain additional information in terms of columns, typically faceting variables.map_dataDescription
A function method for to create the
PANELvariable in layer data. ThePANELvariable is a special variable that tracks the relationship between rows in the layer data and the panels described in thelayoutinput.In addition, #' this function may copy or discard rows as needed, for example when adding margins in FacetGrid.
Usage
Facet$map_data(data, layout, params)Arguments
dataA list of data frames containing layer data.
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.paramsA list of parameters coming from the
setup_params()method.
Value
A list of data frames containing layer data including a
PANELvariable.init_scalesDescription
A function method for initialising position scales. Given a prototype scale for
xandy, creates layout specific scales to accommodate the relationships between panels and scales. By default, the prototype scales are cloned for eachSCALE_XandSCALE_Ylevel. The function is called separately; once forxand once fory.Usage
Facet$init_scales(layout, x_scale, y_scale, params)Arguments
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scale,y_scaleA position scale for the
xandyaesthetics respectively.paramsA list of parameters coming from the
setup_params()method.
Value
A named list with
xandyelements containing a list of panel scales for eachSCALE_Xand/orSCALE_Ylevel respectively.train_scalesDescription
A function method for training position scales. The default trains each scale on the data related to its panels.
Usage
Facet$train_scales(x_scales, y_scales, layout, data, params)Arguments
x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.dataA list of data frames containing layer data.
paramsA list of parameters coming from the
setup_params()method.
Value
Nothing, this method is called for its side-effect of training the scales.
setup_panel_paramsDescription
A function method as a hook to give facets input over panel parameters. By default, returns panel parameters unaltered.
Usage
Facet$setup_panel_params(panel_params, coord, ...)Arguments
panel_paramsA named list of view scales, ranges and other optional parameters from
Coord$setup_panel_params().coordA
<Coord>ggproto object....Currently not in use. For future expansion.
Value
A list of panel parameters.
finish_dataDescription
A function method as a hook for making last-minute modifications to layer data before it is rendered by Geoms. The default is to not modify the data.
Usage
Facet$finish_data(data, layout, x_scales, y_scales, params)Arguments
dataA data frame containing layer data of a single layer.
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.paramsA list of parameters coming from the
setup_params()method.
Value
A data frame containing layer data.
draw_panel_contentDescription
A function method to assemble the panel contents. It delegates the
draw_back()anddraw_front()methods, as well asCoord$draw_panel().Usage
Facet$draw_panel_content( panels, layout, x_scales, y_scales, ranges, coord, theme, params, ... )Arguments
panelsA list parallel to layers. Each element is another list with grobs for each panel, generated by
Layer$draw_geom().layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.rangesA list of panel parameters from the
setup_panel_params()augmented with position guides.coordA
<Coord>ggproto object.dataA list of data frames containing layer data.
themeA complete theme object.
paramsA list of parameters coming from the
setup_params()method....Currently not in use.
Value
A list of grobs, one for each level of the
PANELlayout variable. Grob can bezeroGrob()to draw nothing.draw_back,draw_frontDescription
A function method draw facet background (back) and foreground (front) for panels. The front and back will sandwich the grobs created by layers. The default methods draw nothing.
Usage
Facet$draw_back(data, layout, x_scales, y_scales, theme, params) Facet$draw_front(data, layout, x_scales, y_scales, theme, params)Arguments
dataA list of data frames containing layer data.
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.themeA complete theme object.
paramsA list of parameters coming from the
setup_params()method.
Value
A list of grobs, one for each level of the
PANELlayout variable. Grob can bezeroGrob()to draw nothing.draw_panelsDescription
A function method that orchestrates the majority of facet drawing. It is responsible for assembling a gtable with panel content decorated with axes and strips. The resulting gtable is the basis for the plot in its entirety. It delegates these tasks to the
init_gtable(),attach_axes()andattach_strips()methods.Usage
Facet$draw_panels( panels, layout, x_scales, y_scales, ranges, coord, data, theme, params )Arguments
panelsA list of grobs, one per panel.
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.rangesA list of panel parameters from the
setup_panel_params()augmented with position guides.coordA
<Coord>ggproto object.dataA list of data frames containing layer data.
themeA complete theme object.
paramsA list of parameters coming from the
setup_params()method.
Value
A
gtableobject.init_gtableDescription
A function method that initiates a gtable object containing panels set at the appropriate
ROWandCOLcells from the layout. The panels are separated by thepanel.spacing.{x/y}spacing.Usage
Facet$init_gtable(panels, layout, theme, ranges, params, aspect_ratio)Arguments
panelsA list of grobs, one per panel.
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.themeA complete theme object.
rangesA list of panel parameters from the
setup_panel_params()augmented with position guides.aspect_ratioA scalar numeric for the panel aspect ratio or
NULLfor no aspect ratio.
Value
A
gtableobject containing panel grobs prefixed with"panel".attach_axesDescription
A function method that renders position guides (axes) and attaches these to the gtable with panels. The default method returns the gtable unaltered.
Usage
Facet$attach_axes(table, layout, ranges, coord, theme, params)Arguments
tableA
gtableobject populated with panels from theinit_gtable()method.layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.rangesA list of panel parameters from the
setup_panel_params()augmented with position guides.coordA
<Coord>ggproto object.themeA complete theme object.
paramsA list of parameters coming from the
setup_params()method.
Value
A
gtableobject.attach_stripsDescription
A function method that renders strips and attaches these to the gtable with panels and axes. The
format_strip_labels()method is used to format the strip text. The default method returns the gtable unaltered.Usage
Facet$attach_strips(table, layout, ranges, coord, theme, params)Arguments
tableA
gtableobject from theattach_axes()method.layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.paramsA list of parameters coming from the
setup_params()method.themeA complete theme object.
Value
A
gtableobject.format_strip_labelsDescription
A function method that formats the text for strips. It is used in the
attach_stripsmethods, but also theget_strip_labels()function. The default method returnsNULL.Usage
Facet$format_strip_labels(layout, params)Arguments
layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.paramsA list of parameters coming from the
setup_params()method.
Value
A list containing a data frame with strip labels.
set_panel_sizeDescription
A function method that enforces the
panel.widthsandpanel.heightstheme settings.Usage
Facet$set_panel_size(table, theme)Arguments
tableA
gtableobject populated by thedraw_panels()method.themeA complete theme object.
Value
The
tableobject, optionally with differentwidthsandheightsproperties.attach_axesDescription
A function method that renders axis titles and adds them to the gtable. The default is to add one title at each side depending on the position and presence of axes.
Usage
Facet$draw_labels( panels, layout, x_scales, y_scales, ranges, coord, data, theme, labels, params )Arguments
panelsA
gtableobject initiated by thedraw_panels()method.layoutA data frame computed by the
compute_layout()method. Typically contains the faceting variables,ROW,COL,PANEL,SCALE_XandSCALE_Yvariables.x_scales,y_scalesA list of panel scales for each
SCALE_XandSCALE_Ylevel respectively.rangesA list of panel parameters from the
setup_panel_params()augmented with position guides.coordA
<Coord>ggproto object.dataA list of data frames containing layer data.
themeA complete theme object.
labelsA named list containing an
xlist andylist. Thexandylists haveprimaryandsecondarylabels.paramsA list of parameters coming from the
setup_params()method.
Value
A
gtableobject.varsDescription
A function method that returns the names of faceting variables. The default method returns an character vector with 0 length.
Usage
Facet$vars()Value
A character vector
Conventions
The object name that a new class is assigned to is typically the same as the
class name. Facet class names are in UpperCamelCase and start with the
Facet* prefix, like FacetNew.
A constructor function is usually paired with a Facet class. The constructor
copies the facet class and populates the params field. The constructor
function name should take the Facet class name and be formatted with
snake_case, so that FacetNew becomes facet_new().
See also
The the new facets section of the online ggplot2 book.
Run vignette("extending-ggplot2"), in particular the "Creating a
new faceting" section.
