facet_gridfacet_grid(facets=. ~ ., margins=FALSE, scales="fixed", space="fixed", labeller="label_value", as.table=TRUE, ...)
Lay out panels in a rectangular/tabular manner.
This page describes facet_grid, see layer and qplot for how to create a complete plot from individual components.
What do you think of the documentation? Please let me know by filling out this short online survey.
Parameters control the appearance of the facet. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.
facets: 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); the formula can also be entered as a string instead of a classical formula objectmargins: logical value, should marginal rows and columns be displayedscales: NULLspace: NULLlabeller: NULLas.table: NULLThis function returns a facet object.
> # faceting displays subsets of the data in different panels > p <- ggplot(diamonds, aes(carat, ..density..)) + + geom_histogram(binwidth = 0.2) > > # With one variable > p + facet_grid(. ~ cut)> p + facet_grid(cut ~ .)
> > # With two variables > p + facet_grid(clarity ~ cut)
> p + facet_grid(cut ~ clarity)
> p + facet_grid(cut ~ clarity, margins=TRUE)
> > qplot(mpg, wt, data=mtcars, facets = . ~ vs + am) Error: number of rows of matrices must match (see arg 2) > qplot(mpg, wt, data=mtcars, facets = vs + am ~ . ) Warning: number of columns of result is not a multiple of vector length (arg 1) Error: number of columns of matrices must match (see arg 2) > > # 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 > qplot(mpg, wt, data=mtcars) + 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: > p <- qplot(mpg, wt, data=mtcars, facets = vs ~ cyl) > > df <- data.frame(mpg = 22, wt = 3) > p + geom_point(data = df, colour="red", size = 2)
> > df2 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(0, 1)) > p + geom_point(data = df2, colour="red", size = 2)
> > df3 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(1, 1)) > p + 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")
> > # You may need to set your own breaks for consitent 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) Warning: Removed 11 rows containing missing values (geom_point). Warning: Removed 7 rows containing missing values (geom_point). Warning: Removed 8 rows containing missing values (geom_point).
> > # Free scales are particularly useful for categorical variables > qplot(cty, model, data=mpg) + + facet_grid(manufacturer ~ ., scales = "free", space = "free")
> # particularly when you reorder factor levels > mpg <- within(mpg, { + model <- reorder(model, cty) + manufacturer <- reorder(manufacturer, cty) + }) > last_plot() %+% mpg + opts(strip.text.y = theme_text())
![]()
What do you think of the documentation? Please let me know by filling out this short online survey.