Learn R Programming

fields (version 17.3)

panelPlot: Tips for a panel plot using base R graphics.

Description

This help section gives some tips to create panels of plots in a row/column layout that can have common axes and a single legend or color bar. It also shows how to customize a single plot in the panel to add additional features. All the code is in base R graphics and so is useful as a graphics tutorial and for the user comfortable with base R coding.

Usage

panelPlotExample()

Arguments

Details

The function is really a fairly elaborate "script" that creates a 3X2 panel of plots. Usually there will not be as much variation among the individual plots. That is the whole point of organizing them in a matrix format! However, the intent is for the user to copy this code and modify it according. The commenting should help in naviagting the different parts. We have resisted making this into a more polished function for two reasons. First, panel plots typically need customization and so handling all kinds of flexibilty would make a general function complex. There are useful frameworks such as ggplot2 and lattice that provide this kind of function but in doing so require some effort to deviate from the default settings. The second reason is our realization that that basic elements of grouping plots together and adding common axes on the outsides is really not too complicated to code up. We implement this as a for loop over the panel plots with simple R coding and base graphics. Making this explicit then helps adding particular features to specific plots in the panel -- an option that we found awkward in higher level packages for creating panel plots. See the comments in our example. For a data science student it is useful to become familar with building a complex figure from a sequwence of simpler graphical elements and to be able to do this using base R graphics.

The most exotic part of this example is the use of the the eval function to evaluate the plot functions for each separate panel. The plotting code is setup as components of a list, plotCode, at the beginning of the script. Within the for loop over the plots these graphics commands are just evaluated. This helps to avoid modifying the code within the for loop keeping it unchanged for different figures. The comments in the function indicate the parts to change for each plot and those that usually are not touched.

Here we just generate trivial vectors to plot although the reader is probably aware that any data object refered to in this function will be looked for in the work space. Or to be more rigorous add some figure specific arguments to the function and just pass the object as an argument. This would help to isolate drawing the figure from stray versions of the data objects in the workspace with the same name.

To run the example use panelPlot() from this package. Unfortunately the default package loading is to strip comments from functions and so panelPlot has its comments removed. You can find the commented version in https://github.com/dnychka/fieldsRPackage/tree/main/fields/R in the file panelPlotExample.R Use this code to make a copy of the function, rename it, then modify as needed. Enjoy!

Examples

Run this code
# run as a function
panelPlotExample()
# see 
# https://github.com/dnychka/fieldsRPackage/tree/main/fields/R/panelPlotExample.R
# for the function with comments 
#
# We usually would dump finsihed figures out to a pdf file using  the pdf output function
# to control the size and reproducibilty.
# E.g.
if (FALSE) {
   pdf("panelPlotExample.pdf", width=8, height=8 )
   panelPlotExample()
   dev.off()
}

Run the code above in your browser using DataLab