Introduction to R:loa

Karl Ropkins

2023-10-19

R:loa

This note provides an brief background and overview introduction to the R package loa.

The lattice1 package that comes with R draws graphs.

loa is short for Lattice Optional extras and Add-ins, and that pretty much describes loa.

This note assumes you already have R. If not but the note is of interest, R is open-source and free-to-distribute, and you can get it from >The R Project.

Background

I originally packaged loa because I had a lot of plot code, most modifications of or extensions to existing lattice code, that I was using and replicating in several projects and I wanted to rationalise things.

The intention was also to automate and simplify some of the data handling needed when lattice plotting2.

Getting loa

Installing loa from CRAN, the main R package archive:

#In R, type:
install.packages("loa")

Installing the developers version:

install.packages("loa", repos="http://R-Forge.R-project.org")

To load loa once it is installed:

library(loa) #or require(loa)

Note: loa automatically also loads lattice so you, as well as loa, have access to lattice.

The loa Inventory

loa version 0.2.48.23 contains:

loa also contains one data set, lat.lon.meuse, and one map, roadmap.meuse, which are used in examples in the loa documentation.

The Datasets

lat.lon.meuse is a data.set containing metal concentrations measured in soil and sediment samples collected from a section of the Meuse floodplain in the Netherlands.

It is a modification of the meuse data.set in the sp package.4 It contains concentrations of four metals (cadmium, copper, lead and zinc), sampling location (latitude, longitude, elevation), and other related information (e.g. soil type) for 155 locations.

roadmap.meuse is a Google Map ‘roadmap’ for the meuse area used as a map layer in off-line GoogleMap() examples.

There are further details on both in the package documentation, see ?lat.lon.meuse or ?roadmap.meuse.

Working with loa

loa uses lattice plotting strategies5.

Plot dimensions are set using a formula as the first argument of the plot command.

For those unfamiliar with the approach, it is probably easiest to think of a lattice plot as a model like lm() but with a formula that maps one term onto a series of axes rather than a series of inputs.

For example, loaPlot() uses a formula structure z ~ x * y | cond, where:

The formula terms may be supplied directly or taken from a source identified using a second optional plot argument data. If supplied, data should be a data.frame or something that can be converted into one using as.data.frame(data), e.g.:

loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse)   

Here, the z term, the cadmium data-series, is used to both color and size points plotted at x (longitude) and y (latitude) locations.

Additional arguments may be used to fine-tune the appearance of the plot, e.g. groups to group data within the plot:

loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse,   
        groups=lat.lon.meuse$soil)              

Here, the plot color-codes the points according to soil (soil type 1-3), also in lat.lon.meuse.

Note: By default, groups supersedes z as the coloring term when set but the points remain size scaled according to z.

By comparison, conditioning by soil type would produce:

loaPlot(cadmium~longitude*latitude|soil, data=lat.lon.meuse,
        layout=c(3,1))  

This plots data of different soil types in discrete plot panels, each, by default, plotted using the same x, y, z and groups plot settings. The standard lattice argument layout is also used to plot the lattice sub-plots (or panels) in a row.

loaPlot

Already introduced above, loaPlot() is a z ~ x * y bubble plot with default color (col) and size (cex) scaling linked to the z term unless groups are set, in which case cex is linked to z and col is linked to groups.

Many of the additional plot arguments are the same as in lattice, but some options have been enhanced or simplified, e.g.:

#fixed col (but automatic z size scale)
#key scale manually extended
loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse,
        col="darkred", key.z.at=c(3,6,9,12,15,18))
#replacing default blue scale with RColorBrewer palette
#adding transparency with alpha
#use color scale key like levelplot
loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse, 
        col.regions="Spectral", alpha=0.5, 
        key.fun=draw.loaColorKey)

Note: The loaPlot() key can include z and groups elements. So, you need to specify which you want to change, hence key.z.at=… (not key.at=…).

Like lattice plots, loaPlot() also accepts other panel arguments to produce other plot types.

For example:

#panel.binPlot
loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse,
        panel=panel.binPlot, breaks=20, statistic=max)
#panel.surfaceSmooth
loaPlot(cadmium~longitude*latitude, data=lat.lon.meuse,
        panel=panel.surfaceSmooth, too.far=0.1)

loaPlot() also allows you to supply multiple z values in form z1 + z2 + … + zn ~ x + y.

Note: This option is not intended to be used with loaPlot() without additional option settings because it would just create over-plotting at each (x, y) point. However, it is useful when used with either z case panelling or specialist panels designed for use with mulitple z inputs, e.g.:

#panel.zcases to condition by z1, z2, etc
loaPlot(cadmium*50+copper*10+lead*2+zinc~longitude*latitude, 
          data=lat.lon.meuse, zlab="Metals",
          panel.zcases = TRUE)
#using the panel.zcasePiePlot and z1, z2, etc 
loaPlot(cadmium*50+copper*10+lead*2+zinc~longitude*latitude, 
          data=lat.lon.meuse, zcaselab="Metals",
          panel=panel.zcasePiePlot)

For further information on loaPlot(), see in-package documentation ?loaPlot.

Other Plots

Other plots in loa include RgoogleMapsPlot(), trianglePlot(), stackPlot() and loaBarPlot():

#RgoogleMapsPlot
RgoogleMapsPlot(cadmium~latitude*longitude, data=lat.lon.meuse,
          map=roadmap.meuse)
#trianglePlot
trianglePlot(cadmium~copper+lead+zinc, data=lat.lon.meuse)
#stackPlot
stackPlot(cadmium*40+copper*5+lead+zinc~dist.m, data=lat.lon.meuse)
#loaBarPlot
loaBarPlot(copper~soil, data=lat.lon.meuse, stat=mean)

For further information on these, see in-package documentation.

Any thoughts, suggestions or problems, please let me know > Karl


  1. Sarkar D (2008). Lattice: Multivariate Data Visualization with R. Springer, New York. ISBN 978-0-387-75968-5, http://lmdvr.r-forge.r-project.org.↩︎

  2. I presented earlier thoughts on this topic in e.g. Ropkins, K., Carslaw, D.C., Munir, S., Chen, H. R. (2012) lattice and RgoogleMaps: A practical framework for the development of new geovisualizations. ASA 2012 JSM. San Diego, US.↩︎

  3. Code handling and outputs may vary with package version.↩︎

  4. Pebesma E, Bivand R (2005). “Classes and methods for spatial data in R.” R News, 5(2), 9-13. https://CRAN.R-project.org/doc/Rnews/., Bivand R, Pebesma E, Gomez-Rubio V (2013). Applied spatial data analysis with R, Second edition. Springer, NY. https://asdar-book.org/.↩︎

  5. This is described in early chapters of Sarkar D (2008). Lattice: Multivariate Data Visualization with R. Springer, New York. ISBN 978-0-387-75968-5, http://lmdvr.r-forge.r-project.org.↩︎

  6. Neuwirth E (2022). RColorBrewer: ColorBrewer Palettes. R package version 1.1-3, https://CRAN.R-project.org/package=RColorBrewer.↩︎

  7. Wood SN (2011). “Fast stable restricted maximum likelihood and marginal likelihood estimation of semiparametric generalized linear models.” Journal of the Royal Statistical Society (B), 73(1), 3-36., Wood S, N., Pya, S”afken B (2016). “Smoothing parameter and model selection for general smooth models (with discussion).” Journal of the American Statistical Association, 111, 1548-1575., Wood SN (2004). “Stable and efficient multiple smoothing parameter estimation for generalized additive models.” Journal of the American Statistical Association, 99(467), 673-686., Wood S (2017). Generalized Additive Models: An Introduction with R, 2 edition. Chapman and Hall/CRC., Wood SN (2003). “Thin-plate regression splines.” Journal of the Royal Statistical Society (B), 65(1), 95-114.↩︎

  8. Loecher M, Ropkins K (2015). “RgoogleMaps and loa: Unleashing R Graphics Power on Map Tiles.” Journal of Statistical Software, 63(4), 1-18. http://www.jstatsoft.org/v63/i04/.↩︎