Skip to contents

This function behaves like ggplot2::facet_grid with default arguments, but has a few extra options. It can draw partial or full axis guides at inner panels, and position scales can be independent.

## Usage

facet_grid2(
rows = NULL,
cols = NULL,
scales = "fixed",
space = "fixed",
axes = "margins",
remove_labels = "none",
independent = "none",
shrink = TRUE,
labeller = "label_value",
as.table = TRUE,
switch = NULL,
drop = TRUE,
margins = FALSE,
strip = strip_vanilla()
)

## Arguments

rows

A set of variables or expressions quoted by vars() and defining faceting groups on the rows or columns dimension. The variables can be named (the names are passed to labeller).

For compatibility with the classic interface, rows can also be a formula with the rows (of the tabular display) on the LHS and the columns (of the tabular display) on the RHS; the dot in the formula is used to indicate there should be no faceting on this dimension (either row or column).

cols

A set of variables or expressions quoted by vars() and defining faceting groups on the rows or columns dimension. The variables can be named (the names are passed to labeller).

For compatibility with the classic interface, rows can also be a formula with the rows (of the tabular display) on the LHS and the columns (of the tabular display) on the RHS; the dot in the formula is used to indicate there should be no faceting on this dimension (either row or column).

scales

A character(1) or logical(1) whether scales are shared across facets or allowed to vary. Interacts with the independent argument. One of the following:

"fixed" or FALSE

Scales are shared across all facets (default).

"free_x"

x-scales are allowed to vary across rows.

"free_y"

y-scales are allowed to vary across columns.

"free" or TRUE

Scales can vary across rows and columns.

space

A character(1) or logical(1) determining whether the size of panels are proportional to the length of the scales. When the independent argument allows for free scales in a dimension, the panel sizes cannot be proportional. Note that the scales argument must be free in the same dimension as the space argument to have an effect.One of the following:

"fixed" or FALSE

All panels have the same size (default).

"free_x"

Panel widths are proportional to the x-scales.

"free_y"

Panel heights are proportional to the y-scales.

"free" or TRUE

Both the widths and heights vary according to scales.

axes

A character(1) or logical(1) where axes should be drawn. One of the following:

"margins" or FALSE

Only draw axes at the outer margins (default).

"x"

Draw axes at the outer margins and all inner x-axes too.

"y"

Draw axes at the outer margins and all inner y-axes too.

"all" or TRUE

Draw the axes for every panel.

remove_labels

A character(1) or logical(1) determining whether axis text is displayed at inner panels. One of the following:

"none" or FALSE

Display axis text at all axes (default).

"x"

Display axis text at outer margins and all inner y-axes.

"y"

Display axis text at outer margins and all inner x-axes.

"all" or TRUE

Only display axis text at the outer margins.

independent

A character(1) or logical(1) determining whether scales can vary within a row or column of panels, like they can be in ggplot2::facet_wrap. The scales argument must be free for the same dimension before they can be set to independent. One of the following:

"none" or FALSE

All y-scales should be fixed in a row and all x-scales are fixed in a column (default).

"x"

x-scales are allowed to vary within a column.

"y"

y-scales are allowed to vary within a row.

"all" or TRUE

Both x- and y-scales are allowed to vary within a column or row respectively.

shrink

If TRUE, will shrink scales to fit output of statistics, not raw data. If FALSE, will be range of raw data before statistical summary.

labeller

A function that takes one data frame of labels and returns a list or data frame of character vectors. Each input column corresponds to one factor. Thus there will be more than one with vars(cyl, am). Each output column gets displayed as one separate line in the strip label. This function should inherit from the "labeller" S3 class for compatibility with labeller(). You can use different labeling functions for different kind of labels, for example use label_parsed() for formatting facet labels. label_value() is used by default, check it for more details and pointers to other options.

as.table

If TRUE, the default, the facets are laid out like a table with highest values at the bottom-right. If FALSE, the facets are laid out like a plot with the highest value at the top-right.

switch

By default, the labels are displayed on the top and right of the plot. If "x", the top labels will be displayed to the bottom. If "y", the right-hand side labels will be displayed to the left. Can also be set to "both".

drop

If TRUE, the default, all factor levels not used in the data will automatically be dropped. If FALSE, all factor levels will be shown, regardless of whether or not they appear in the data.

margins

Either a logical value or a character vector. Margins are additional facets which contain all the data for each of the possible values of the faceting variables. If FALSE, no additional facets are included (the default). If TRUE, margins are included for all faceting variables. If specified as a character vector, it is the names of variables for which margins are to be created.

strip

An object created by a call to a strip function, such as strip_vanilla.

## Value

A Facet ggproto object that can be added to a plot.

## Details

Both the independent and space arguments only have an effect when the scales argument in a dimension is free. However, the independent and space arguments can not be used to simultaneously set an independent scale and have the panel size be proportional to that scale.

## See also

Other facetting functions: facet_manual(), facet_nested_wrap(), facet_nested(), facet_wrap2()

## Examples

p <- ggplot(mpg, aes(displ, hwy)) + geom_point()

# Repeat all axes for every facet
p + facet_grid2(cyl ~ drv, axes = "all")

# Repeat only y-axes
p + facet_grid2(cyl ~  drv, axes = "y")

# Repeat axes without x-labels
p + facet_grid2(cyl ~ drv, axes = "all", remove_labels = "x")

# Grid facets with independent axes for every panel
p + facet_grid2(cyl ~ drv, scales = "free", independent = "all")