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.
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()
)
A Facet
ggproto object that can be added to a plot.
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).
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.
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.
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.
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.
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.
If TRUE
, will shrink scales to fit output of
statistics, not raw data. If FALSE
, will be range of raw data
before statistical summary.
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.
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.
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"
.
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.
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.
An object created by a call to a strip function, such as
strip_vanilla
.
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.
Other facetting functions:
facet_manual()
,
facet_nested_wrap()
,
facet_nested()
,
facet_wrap2()
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")
Run the code above in your browser using DataLab