How do ggplot2 arguments work?

ggplot2, r

Solution

Hadley, the creator of ggplot2, has a wonderful entry on the devtools wiki about building your own version of the `subset` function--which takes a data argument as well. I learned a whole lot reading it, and I'd be willing to bet the mechanics built up in the article are very similar to what `ggplot` does.

https://github.com/hadley/devtools/wiki/Evaluation

Problem

In R with `ggplot2`, these two lines seem to do the same thing: ``` qplot(data=diamonds, carat, price) qplot(data=diamonds, get("carat"), get("price")) ``` but I don't understand how they work... How does R understand what `carat` refers to in the first case and what `get("carat")` refers to in the second? If I just try to access `carat` or `get("carat")` on their own I get (unsurprisingly) ``` > carat Error: object 'carat' not found > get("carat") Error in get("carat") : object 'carat' not found ``` Under the hood, how does `ggplot2` set up the bindings such that these elegant calling semantics "just work" and more specifically, how would I go about implementing this sort thing myself? I've tried to read the source but am having trouble understanding it.

Original source