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.
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.
Installing loa from CRAN, the main R package archive:
#In R, type: install.packages("loa")
Installing the developers version:
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.
loa version 0.2.47.13 contains:
loa also contains one data set, lat.lon.meuse, and one map, roadmap.meuse, which are used in examples in the loa documentation.
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.
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:
x and y are the x and y axes,
z is an optional term controlling the appearance (color and size) of points plotted at (x, y), and
cond, also optional, is any additional term that can be used to subsample (x,y,z) data when plotting subsets separately.
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.:
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.
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)
As in lattice, col sets point color directly and col.regions sets the color palette colors are selected from. But loa also uses RColorBrewer6 to handle color ranges. So, the loa version of col.regions is a little more flexible than in lattice. For example, loaPlot() handles arguments like col.regions = c(“green”, “red”).
unlike xyplot() in lattice, loaPlot() automatically generates a plot key if it thinks you need one, but (like in lattice), key and key… arguments can be used to modify the key, e.g. key = FALSE to remove the key.
I have tried to simplify some common arguments that use list structures in lattice. So, e.g. instead of using key=list(z=list(at=c(…))) you can use key.z.at=c(…) to manually set the points shown in the key. There are trade-offs in doing this, so it is currently only applied to selected cases in loaPlot(), but it does mean you can more easily change grid appearance with e.g. grid.col=2 or grid.lty=3, etc.
I also linked the loaPlot() argument zlab to key.z.main so you could rename the z key like you would the x and y axes.
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.
#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 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
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.↩︎
Code handling and outputs may vary with package version.↩︎
Pebesma EJ, Bivand RS (2005). “Classes and methods for spatial data in R.” R News, 5(2), 9-13. <URL: https://CRAN.R-project.org/doc/Rnews/>., Bivand RS, Pebesma E, Gomez-Rubio V (2013). Applied spatial data analysis with R, Second edition. Springer, NY. <URL: https://asdar-book.org/>.↩︎
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.↩︎