concept facet_grid() in category R

appears as: facet_grid()
Exploring Data with R

This is an excerpt from Manning's book Exploring Data with R.

Facets are a way of splitting a single plot into multiple subplots. The splitting of the dataset is based on a grouping (or a combination of groups). In this way, we get a set of panels where each panel displays a different subset of the data. This is great for comparisons across groupings and, by default, each of the panels will have fixed coordinates (i.e., common scales: we can make easy comparisons of data point values across panels). There are two functions in ggplot that let us create faceted plots: facet_wrap() and facet_grid().

When faceting by two variables, the use of facet_grid() might result in a better appearance of panels (Listing 3.15). As the name of the function implies, panels are placed into a strict grid. The faceting variables provide the x and y positions of the panels. Let’s rework the plot to the grid layout to demonstrate this. The changes to make are to use facet_grid() instead of facet_wrap() and, within that, use the rows and cols arguments (both with vars()) to tell ggplot which faceting variables should run across rows or columns (Figure 3.15).

Figure 3.15. Faceting by two categorical variables with facet_grid().
Listing 3.15 Using `facet_grid()` provides a slightly different visualization of the faceted plot panels.
```{r dmd_facet_cut_clarity_grid}
ggplot(dmd, aes(x = carats, y = price)) +
  geom_point(alpha = 0.2) +
  facet_grid(rows = vars(cut), cols = vars(clarity), labeller = label_both)/#A
```

This arrangement makes is slightly easier to see that the rows of plots represent different values of cut. The choice in whether to use facet_grid() over facet_wrap() is often a matter of taste or practicality. When faceting by a single variable, facet_wrap() might be the best way. If there are two faceting variables, try both facet_wrap() and facet_grid() and then make a call on which approach to faceting works best for that case. Until you develop a better feel for how these faceting options work, don’t be afraid to experiment with both functions.