Unify R plots with pander
[This article was first published on rapporter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
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.
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
First, let us check out how a default histogram looks like in the major graphics packages applied to
Please note that I have passed
Doing the same with
I hope you would find the above graphs pretty similar 🙂
For a more detailed view on the results of
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) |
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")') |
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 their blog: rapporter.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.