R tips 2: Writing your own stat_ for ggplot2

Last evening I wrote a small package containing a new stat_poly_eq() based on my previous R tips 1: Playing with polynomials.

It is simple enough that it may serve as an example of what is need under ggplot2 >= 2.0.0 for defining a simple stat_. I used the source code of ggplot2 itself as a template, maybe you could just use mine given below. For each stat one needs to define one function, which will be public and an object holding all the definitions needed for the actual computations and mapping of aesthetics (the object is used internally although still needs to be exported if included in a package).

The knitted example and the Rmarkdown code are available as separate files.

A package that as of today contains the same stat_poly_eq() definition is available at Bitbucket. In the future I may add more examples or improve this one. It can be installed from source using package devtools.

[code lang=”r”]devtools::install_bitbucket(“aphalo/ggpmisc”)[/code]

Share on

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.