-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Mixing ggplot2 graphs with other graphical output
ggplot2 is based on Grid graphics, and is therefore mostly incompatible with standard R graphics functions. In particular,
-
par()is without effect to specify a layout (mfrow, etc.) - idem for
layout()andsplit.screen() - standard R graphics such as
plot(),image(),hist()will not easily be placed on the same device as a ggplot2 graph. ThegridBasepackage may offer some help in this regard, however.
The Grid graphics system is more flexible and powerful than standard R graphics, however, and many of the base functions are available in one form or another in the grid, lattice, or vcd packages. A couple of additional Grid functions is available in the gridExtra and RGraphics packages.
ggplot2 has a powerful facetting system that allows complex bidimensional arrangements of plot panels on a single page. In some cases, however, it is useful to place side-by-side several different plots not linked by facetting. It can also be the case that one wishes to add an inset to a larger plot. In all these cases, ggplots have to be placed on the page using the Grid viewport system.
The gridExra package provides a wrapper function for the most common case, where one wishes to arrange several plots on a rectangular grid,
testThe ggExtra package provides a few examples of geoms that can be used to annotate a ggplot2 with an external graphical element ("grob", in Grid terminology), such as a table or an image.
A basic text summary can be added next to a ggpot2 using viewports. Useful functions to add text to a graphic device in the Grid graphics framework include the tableGrob function in package gridExtra and splitTextGrob in package RGraphics.