I\'ve been reading the ggplot2
documentation for both functions. I was wondering what were the differences and what would be right situation for using each func
Quoting mainly from ggplot2 book, p. 148f.
There are three types of facetting:
facet_null()
: a single plot, the default.facet_wrap()
: "wraps" a 1d ribbon of panels into 2d.facet_grid()
: produces a 2d grid of panels defined by variables which form
the rows and columns.Facet wrap
facet_wrap()
makes a long ribbon of panels (generated by any number of
variables) and wraps it into 2d. This is useful if you have a single variable
with many levels and want to arrange the plots in a more space efficient
manner.
You can control how the ribbon is wrapped into a grid with ncol
, nrow
,
as.table
and dir
. ncol
and nrow
control how many columns and rows (you only need to set one). as.table
controls whether the facets are laid out like
a table (TRUE
), with highest values at the bottom-right, or a plot (FALSE
),
with the highest values at the top-right. dir
controls the direction of wrap:
horizontal or vertical.
Facet grid
From ?facet_grid
: facet_grid()
forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.
You can use multiple variables in the rows or columns, by "adding" them
together, e.g. a + b ~ c + d
.
facet grid()
has an additional parameter called space
, which takes the
same values as scales
.
# If scales and space are free, then the mapping between position
# and values in the data will be the same across all panels. This
# is particularly useful for categorical axes
ggplot(subset(mpg, manufacturer %in% c("audi", "honda", "toyota")) , aes(drv, model)) +
geom_point() +
facet_grid(manufacturer ~ ., scales = "free", space = "free") +
theme(strip.text.y = element_text(angle = 0))
( simplified ) Example taken from ?facet_grid