How does "+" work in ggplot2?

ggplot2, r

Solution

`methods("+")` will show a starred `"+.gg*"`, non-exported from ggplot2.

ggplot2:::`+.gg` 

will reveal the beast.

Edit: to clarify, `+` is a generic function in base R, and anyone can define their own method for a specific class. The ggplot2 package internally defines it for objects of class `gg`; it is not exported in the package Namespace so a little convincing is required to expose its source code, e.g. with `getAnywhere("+.gg")`.

Problem

This is a seemingly simple question that I'm asking for the sake of better understanding how domain specific languages work in R. How does the generic function "+" allow one to build a layered plot in `ggplot2`? Obviously `ggplot2` works at a very high level of abstraction. Could someone show a simple example showing how "+" works under the hood with `ggplot2`?

Original source