concept facet_wrap() in category R

This is an excerpt from Manning's book Exploring Data with R MEAP V05.
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().
The diamonds described in the dmd dataset have discrete variables that are useful for faceting: color, cut, and clarity. What if we could make our plot of price vs. carats for each of the three cases of clarity (e.g., diamonds with Fair clarity in the first plot, and similarly diamonds with Great and The Best clarity in the second and third plots)? What if these plots could all appear together as a combined graphic? That there is faceting! So let’s take the earlier dmd_carats_price code (Listing 3.2) and apply the facet_wrap() function (Listing 3.11) to facet by clarity, giving us Figure 3.11.
Figure 3.12. Faceting by two categorical variables with facet_wrap().
![]()