This function adds position scales (x and y) of individual panels. These can be used to fine-tune limits, breaks and other scale parameters for individual panels, provided the facet allows free scales.
scale_x_facet(expr, ..., type = "continuous") scale_y_facet(expr, ..., type = "continuous")
Other arguments passed to the scale.
These scale functions work through the mechanism of the
facetted_pos_scales() function, and the same limitations apply: scale
transformations are applied after
stat transformations, and the
argument of scales is ignored.
expr argument, the expression will be evaluated in the context
of the plot's layout. This is an internal
data.frame structure that
isn't normally exposed, so it requires some extra knowledge. For most
facets, the layout describes the panels, with one panel per row. It
PANEL columns that keep track of what
panel goes where in a grid of cells. In addition, it contains the
facetting variables provided to the
of the facets. For example, if we have a plot facetted on the
variable with the levels
C, as 1 row and 3 columns, we might
target the second
B panel with any of these expressions:
var == "B",
PANEL == 2 or
COL == 2. We can inspect the layout structure by using
p is a plot.
When using multiple
expr argument can target
the same panels. In such case, the scales added to the plot first overrule
the scales that were added later.
scale_facet object that can be added to a plot.
# A standard plot with continuous scales p <- ggplot(mtcars, aes(disp, mpg)) + geom_point() + facet_wrap(~ cyl, scales = "free") # Adding a scale for a value for a facetting variable p + scale_x_facet(cyl == 8, limits = c(200, 600)) # Adding a scale by position in the layout p + scale_x_facet(COL == 3, limits = c(200, 600)) # Setting the default scale and making an exception for one panel p + scale_y_continuous(limits = c(0, 40)) + scale_y_facet(PANEL == 1, limits = c(10, 50)) # Using multiple panel-specific scales p + scale_y_facet(PANEL == 1, limits = c(10, 50)) + scale_y_facet(cyl == 6, breaks = scales::breaks_width(0.5)) # When multiple scales target the same panel, the scale added first gets # priority over scales added later. p + scale_y_facet(COL == 2, limits = c(10, 40)) + scale_y_facet(cyl %in% c(4, 6), breaks = scales::breaks_width(1)) # A standard plot with discrete x scales p <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot() + facet_wrap(~ vs, scales = "free") # Expanding limits to show every level p + scale_x_facet(vs == 1, limits = factor(c(4, 6, 8)), type = "discrete") # Shrinking limits to hide a level p + scale_x_facet(vs == 0, limits = factor(c(4, 6)), type = "discrete")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.