Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set number of columns (or rows) in a facetted plot

Tags:

r

ggplot2

I have a facetted plot like this:

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  facet_grid(. ~ carb)

enter image description here 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 :-)

like image 571
user246211 Avatar asked Jan 21 '10 10:01

user246211


People also ask

What is the function of Facet_grid () in Ggplot ()?

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.

What is a faceted plot?

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.

How does facet wrap work r?

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.


1 Answers

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)

enter image description here

like image 147
George Dontas Avatar answered Sep 28 '22 15:09

George Dontas