Combining base and ggplot2 graphics in R

advertisement
Skill sharing: Combining
base and ggplot2 graphics
in R.
GLEON Fellowship Workshop
January 14-18, 2013
Sunapee, NH
Richard Iestyn Woolway
Plotting with R
• R provides several frameworks for composing figures.
• The simplest is the base graphics.
• Another plotting system of R is ggplot2, which allows users to create
publishable figures with ease.
• Has a good book explaining the philosophy behind it:
http://ggplot2.org/
Plotting with R – base graphics
• I recently needed to create a multi panel figure composed of a line
plot, continuous wavelet transform, and the autocorrelation of a time
series .
• This can easily be created in base graphics using either par() or
layout().
• I then decided to use ggplot2 to improve the quality of the acf plot in
the third panel of the figure.
Plotting with R – ggplot2 graphics
• We can improve the quality of the acf plot with ggplot2:
• However, as par() and layout() are only available with base
graphics, we cannot mix base and ggplot graphics in a single figure
window.
• The most appropriate solution is to use the gridBase package.
• The script called ‘RIW_code.R’ shows how gridBase is used.
Integration of base and ggplot graphics
• gridBase provides similar functionality to par(mfrow=c(x,y)).
• This is an useful technique when we wish to create multiple panel
figures that combines plots based on base graphics (e.g. biplots),
that have not been implemented in grid (i.e. ggplot), and the task of
re-implementing in grid is extremely daunting.
Download