Unify R plots with pander

August 7, 2012
By

(This article was first published on rapporter, and kindly contributed to R-bloggers)

Motivation

R has a great variety of plotting tools (just to mention a few: the base graphics and e.g. lattice and ggplot2 packages building on grid) and most R user has a preference for either of them.

I think all of you would agree with me: each package has its advantages and also disadvantages compared to the others. And any senior R user could compile a lengthy list about this topic. But we do not intend to start a flame-war here :)

So I do not share my personal preferences in this post despite the fact that I was really puzzled while developing pander: which graphics package should be supported with configurable options? As I planned to provide some panderOptions in which the user could specify e.g. a color palette or the font family to be used in all of his plots. But which library is used by most of R users? And how many potential users would I loose by choosing that only one?

In short, I decided to support all major graph engines, which means letting users specify their custom options and apply those to all graphics, lattice and ggplot2 calls. This decision was a fast and promising one, the development was rather cumbersome.

Implementation

I came up with the idea to tweak evals further and apply options while evaluating R commands. This solution has the advantage of also tweaking e.g. par before the actual commands (and even the plotting functions of graphics too), but evals got even more bloated. Well, I decided to take that trouble.

Now we have a bunch of options available if graph.unify is enabled in evalsOptions (disabled by default not to freak out newcomers). You can fine-tune (panderOptions) the foreground, background and other color palettes, the global font size and even the font family used, the grid with optional minor ticks (even in base plots), the legend position and the angle of axis labels besides some other small tweaks.

I will only concentrate on the results below to keep this post short, if you would be interested in the sources, please check out the relevant branch on GitHub.

Results

To show a brief demo of the new options, let us load the package and enable graph.unify for evals:

library(pander)
evalsOptions('graph.unify', TRUE)

First, let us check out how a default histogram looks like in the major graphics packages applied to horsepower in mtcars:
hist(mtcars$hp)
histogram(mtcars$hp)

ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50)
Please note that I have passed binwidth to the ggplot2 call not to abuse the comparison.

Doing the same with evals (what is the default if you are using pander's custom brew engine) results in:
evals('hist(df$hp, main = "Histogram in base R")')


evals('histogram(df$hp, main = "Histogram with lattice")')


evals('ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50)
+ opts(title = "Histogram with ggplot2")')
I hope you would find the above graphs pretty similar :)

For a more detailed view on the results of unify.images, please check out the package bundled demo (beside others) or the sources of the graphs.brew file.

To leave a comment for the author, please follow the link and comment on his blog: rapporter.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.