R 3.3.2 has been released

It is always best to keep R up-to-date. This is not a major release, but instead mostly a bug fix release.

If you are using R on MS-Windows then you can very painlessly upgrade R with package ‘installr’. This are the needed steps, that should be carried not in RStudio but in RGUI (i.e. double click the R icon on your desktop).

At the console enter:

install.packages("installr") # install
setInternet2(TRUE) # only for R versions older than 3.3.0
installr::updateR() # updating R.


Follow the prompts. I suggest that you copy and update the packages in the library.

R tips 3: ggpmisc adds new stats to ‘ggplot2’

Aim of the package

  1. Make it possible to use the grammar of graphics in some cases for which until now ad-hoc solutions were needed.
  2. Show how easy it can be to write new statistics for ‘ggplot2 (>= 2.0.0)’.

Case 1 (Stackoverflow question)

“Adding a 3rd order polynomial and its equation to a ggplot in r”

Re-stated problem

“Adding a label with the R^2 or adjusted R^2 value from any lm() fit for each group and panel of a ggplot”

“Adding a polynomial of any degree and its equation for each group and panel of a ggplot”

Continue reading

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]

R tips 1: playing with polynomials

Polynomial equation as annotation

Polynomial equation as annotation to a ggplot.

I have collected some code snippets I used recently for working with polynomials. The idea of writing this page came from a question I was asked a few days ago by Titta Kotilainen. I should emphasize that the page linked below concerns only polynomials, and excludes all other linear models that can be fit with lm(). Even models that are polynomials but formulated not as a regular polynomial equation (e.g. x ~ I(x^2) + x instead of the expected x + I(x^2) will quietly fail! In contrast, use of poly() as in y ~ poly(x, 2) is fine.

I used Rmarkdown in RStudio to generate a page with knitted examples, if interested the source file is also available.