[R] [solved!] Re: [lattice] format and rotation of strip text

Tom Roche Tom_Roche at pobox.com
Tue Nov 20 19:24:45 CET 2012


https://stat.ethz.ch/pipermail/r-help/2012-November/329509.html
>> -  without the line above commented out, strip values are (correctly)
                                typo, should be "incorrectly" ^^^^^^^^^
>>    all 1.123

>> +  with the line above commented out, strip values are (correctly)
>>    1.123 .. 5.123

>> how to round() or signif() the values obtained from array.3d.df$lev
>> and displayed in the strips?

https://stat.ethz.ch/pipermail/r-help/2012-November/329512.html
> [omitted] seems to be a better result:

Indeed! The complete sequence that Worked For Me is

# start example
library(reshape2)
library(lattice)

lon=11
lat=7
lev=5
len=lon*lat*lev
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
# desired, please let me know: I know 'for' loops are deprecated
# in R.

i=1
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)
array.3d[,,1]
array.3d[,,lev]

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

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

array.3d.df$lev <- array.3d.df$lev + 0.12345 # truncated below, and ...
# ... below note output from these
head(array.3d.df)
tail(array.3d.df)

# 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

levelplot(
  conc ~ lon * lat | rev(lev), data=array.3d.df, layout=c(1,lev),
  levs=as.character(round(array.3d.df[['lev']], 1)),
  strip=FALSE,
  strip.left=strip.custom(
    factor.levels=as.character(signif(unique(array.3d.df[['lev']]), 3)),
    strip.levels=TRUE,
    horizontal=TRUE,
    strip.names=FALSE,
    # gotta shrink strip text size to fit strip width:
    # more on that separately
    par.strip.text=list(cex=0.5)
  )
)
# end example

If there's a lattice wiki or other way to user-contribute visualization
documentation, please lemme know. (For that matter, why is there not
something like an r-sig-vis?)

Your assistance is appreciated! Hoping this will be useful to others,
Tom Roche <Tom_Roche at pobox.com>




More information about the R-help mailing list