facet_grid(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE)
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."fixed"
), or do they vary across rows ("free_x"
),
columns ("free_y"
), or both rows and columns ("free"
)"fixed"
, the default, all panels have the same size.
If "free_y"
their height will be proportional to the length of the
y scale; if "free_x"
their width will be proportional to the
length of the x scale; or if "free"
both height and width will
vary. This setting has no effect unless the appropriate scales also vary.TRUE
, will shrink scales to fit output of
statistics, not raw data. If FALSE
, will be range of raw data
before statistical summary.~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()
. See
label_value
for more details and pointers to other
options.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."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"
.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.Lay out panels in a grid.
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() # With one variable p + facet_grid(. ~ cyl)
p + facet_grid(cyl ~ .)
# With two variables p + facet_grid(vs ~ am)
p + facet_grid(am ~ vs)
p + facet_grid(vs ~ am, margins=TRUE)
# To change plot order of facet grid, # change the order of variable levels with factor() set.seed(6809) diamonds <- diamonds[sample(nrow(diamonds), 1000), ] diamonds$cut <- factor(diamonds$cut, levels = c("Ideal", "Very Good", "Fair", "Good", "Premium")) # Repeat first example with new order p <- ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 1) p + facet_grid(. ~ cut)
g <- ggplot(mtcars, aes(mpg, wt)) + geom_point() g + facet_grid(. ~ vs + am)
g + facet_grid(vs + am ~ .)
# You can also use strings, which makes it a little easier # when writing functions that generate faceting specifications p + facet_grid("cut ~ .")
# see also ?plotmatrix for the scatterplot matrix # If there isn't any data for a given combination, that panel # will be empty g + facet_grid(cyl ~ vs)
# If you combine a facetted dataset with a dataset that lacks those # facetting variables, the data will be repeated across the missing # combinations: g + facet_grid(vs ~ cyl)
df <- data.frame(mpg = 22, wt = 3) g + facet_grid(vs ~ cyl) + geom_point(data = df, colour = "red", size = 2)
df2 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(0, 1)) g + facet_grid(vs ~ cyl) + geom_point(data = df2, colour = "red", size = 2)
df3 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(1, 1)) g + facet_grid(vs ~ cyl) + geom_point(data = df3, colour = "red", size = 2)
# You can also choose whether the scales should be constant # across all panels (the default), or whether they should be allowed # to vary mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() mt + facet_grid(. ~ cyl, scales = "free")
# If scales and space are free, then the mapping between position # and values in the data will be the same across all panels mt + facet_grid(. ~ cyl, scales = "free", space = "free")
mt + facet_grid(vs ~ am, scales = "free")
mt + facet_grid(vs ~ am, scales = "free_x")
mt + facet_grid(vs ~ am, scales = "free_y")
mt + facet_grid(vs ~ am, scales = "free", space = "free")
mt + facet_grid(vs ~ am, scales = "free", space = "free_x")
mt + facet_grid(vs ~ am, scales = "free", space = "free_y")
# You may need to set your own breaks for consistent display: mt + facet_grid(. ~ cyl, scales = "free_x", space = "free") + scale_x_continuous(breaks = seq(10, 36, by = 2))
# Adding scale limits override free scales: last_plot() + xlim(10, 15)Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale. Warning message: Removed 26 rows containing missing values (geom_point).
# Free scales are particularly useful for categorical variables ggplot(mpg, aes(cty, model)) + geom_point() + facet_grid(manufacturer ~ ., scales = "free", space = "free")
# particularly when you reorder factor levels mpg$model <- reorder(mpg$model, mpg$cty) manufacturer <- reorder(mpg$manufacturer, mpg$cty) last_plot() %+% mpg + theme(strip.text.y = element_text())
# Use as.table to to control direction of horizontal facets, TRUE by default h <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() h + facet_grid(cyl ~ vs)
h + facet_grid(cyl ~ vs, as.table = FALSE)
# Use labeller to control facet labels, label_value is default h + facet_grid(cyl ~ vs, labeller = label_both)
# Using label_parsed, see ?plotmath for more options mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)")) k <- ggplot(mtcars, aes(wt, mpg)) + geom_point() k + facet_grid(. ~ cyl2)
k + facet_grid(. ~ cyl2, labeller = label_parsed)
# For label_bquote the label value is x. p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() p + facet_grid(. ~ vs, labeller = label_bquote(alpha ^ .(x)))Warning message: Referring to `x` is deprecated, use variable name instead
p + facet_grid(. ~ vs, labeller = label_bquote(.(x) ^ .(x)))Warning message: Referring to `x` is deprecated, use variable name instead
# Margins can be specified by logically (all yes or all no) or by specific # variables as (character) variable names mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() mg + facet_grid(vs + am ~ gear)
mg + facet_grid(vs + am ~ gear, margins = TRUE)
mg + facet_grid(vs + am ~ gear, margins = "am")
# when margins are made over "vs", since the facets for "am" vary # within the values of "vs", the marginal facet for "vs" is also # a margin over "am". mg + facet_grid(vs + am ~ gear, margins = "vs")
mg + facet_grid(vs + am ~ gear, margins = "gear")
mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
# The facet strips can be displayed near the axes with switch data <- transform(mtcars, am = factor(am, levels = 0:1, c("Automatic", "Manual")), gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five")) ) p <- ggplot(data, aes(mpg, disp)) + geom_point() p + facet_grid(am ~ gear, switch = "both") + theme_light()
# It may be more aesthetic to use a theme without boxes around # around the strips. p + facet_grid(am ~ gear + vs, switch = "y") + theme_minimal()
p + facet_grid(am ~ ., switch = "y") + theme_gray() %+replace% theme(strip.background = element_blank())