# 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")
Run the code above in your browser using DataLab