I have a facetted plot like this:
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(. ~ carb)
However, the graph is too wide to be clearly read.
I'd like to be able to take the three rightmost locations and place them under the three leftmost, i.e. the facets should be in three columns * two rows like this.
1 2 3 4 5 6
Is it possible to set the layout of the facets, i.e. to set number of columns (or rows) with facet_grid()
?
The documentation on facet_grid doesn't seem to indicate that it's possible.
Thanks for the help :-)
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.
Facet plots, also known as trellis plots or small multiples, are figures made up of multiple subplots which have the same set of axes, where each subplot shows a subset of the data.
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.
You can use the ncol
(or nrow
) argument in facet_wrap
:
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_wrap(~ carb, ncol = 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With