Skip to content

Mixing ggplot2 graphs with other graphical output

baptiste edited this page Sep 2, 2010 · 10 revisions

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() and split.screen()
  • standard R graphics such as plot(), image(), hist() will not easily be placed on the same device as a ggplot2 graph. The gridBase package 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.

Layout multiple ggplots on a single page

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,

Place a foreign graphical element inside a ggplot

The 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.

Add tables and/or text summary beside a ggplot

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.

Note: The ggplot2 wiki is no longer maintained, please use the ggplot2 website instead!

Clone this wiki locally