[R] [lattice] format and rotation of strip text

Tom Roche Tom_Roche at pobox.com
Sun Nov 18 21:42:45 CET 2012

Thanks to the lattice gurus on this list, and having reference to the
excellent open-access Sarkar 2008

ISBN 978-0-387-75968-5
e-ISBN 978-0-387-75969-2

I now know how to label lattice panels by variable value: see thread
starting @


(and demonstrated below). This allows me to use lattice::levelplot() to
display atmospheric data (gas concentrations) over a 3D space with
dimensions longitude, latitude, and (vertical) level ... but I would
like to fix a few things. Hopefully the following is a sufficiently
"small, self-contained example" (though it has 2 side questions
injected--answers to those are appreciated also):

# start example

array.3d <- array(data=c(1:len), dim=c(lat, lon, lev))

# Rewrite the array values "more spatially," i.e., row-wise from
# bottom left. If there's a more-R-ish way to fill this array
# as specified, please let me know: I know 'for' loops are deprecated
# in R.

for (z in 1:lev) {
  for (x in lat:1) {
    for (y in 1:lon) {
      array.3d[x,y,z]=i ; i=i+1

# produces (with rows=latitudes and cols=longitudes)

# convert data=array.3d to dataframe with reshape2::melt
array.3d.df <- melt(array.3d, varnames=c("lat","lon","lev"), value.name="conc")

# make level values {longer, "more realistic"}

array.3d.df$lev <- array.3d.df$lev + 0.12345

# plot "appropriately" for atmospheric data where lev=pressure: use
# * lattice::levelplot
# * one column, since atmospheric levels stack vertically
# * rev(lev), since layers closer to ground level have higher pressure
  conc ~ lon * lat | rev(lev), data=array.3d.df,
  layout=c(1,lev),  # show levels stacked in 1 vertical column
  strip=FALSE,      # this suppresses printing strips atop packets
    strip.levels=TRUE,  # print level values
    strip.names=FALSE   # don't print name of level variable="rev(lev)"
# end example

Note that the (colored) 'strip' for each panel in the lattice has

- the corresponding layer value printed inside curly brackets, e.g.,

- the layer value printed in full

- the layer value rotated 90° CCW (like the y-axis label)

I would prefer to have

+ the layer value *not* printed inside curly brackets

+ the layer value *not* rotated 90° CCW (i.e., to print the layer value
  like the x-axis label)

+ the layer value truncated or rounded to some significant digits,
  e.g., '1.1'

I suspect this can be done with strip.custom, but am not seeing how;
please enlighten!

Your assistance is appreciated, Tom Roche <Tom_Roche at pobox.com>

More information about the R-help mailing list