20.7 New facets
Facets are one of the most powerful concepts in ggplot2, and it follows that extending facets is one of the most powerful ways to modify how ggplot2 operates. All that power comes at a cost, though. Facets are responsible for recieving all the different panels, attching axes (and strips) to them, and arranging them in the expected manner. All of this requires a lot of gtable manipulation and grid understanding and can be a daunting undertaking.
Depending on what you want to achieve, you may be able to skip that almost completely. If your new facet will end up with a rectangular arrangement of panels, it is often possible to subclass either FacetWrap
or FacetGrid
, and simply provide new compute_layout()
, and map_data()
methods. The first takes care of recieving the original data and create a layout specification, while the second recieves the created layout along with the data, and attaches a PANEL
column to it, mapping the data to one of the panels in the layout. An example of this type of subclassing will be given in a later chapter.