str(xscale.components.default(c(0, 1)))
set.seed(36872)
rln <- rlnorm(100)
densityplot(rln,
scales = list(x = list(log = 2), alternating = 3),
xlab = "Simulated lognormal variates",
xscale.components = function(...) {
ans <- xscale.components.default(...)
ans$top <- ans$bottom
ans$bottom$labels$labels <- parse(text = ans$bottom$labels$labels)
ans$top$labels$labels <-
if (require(MASS))
fractions(2^(ans$top$labels$at))
else
2^(ans$top$labels$at)
ans
})
## Direct use of axis to show two temperature scales (Celcius and
## Fahrenheit). This does not work for multi-row plots, and doesn't
## do automatic allocation of space
F2C <- function(f) 5 * (f - 32) / 9
C2F <- function(c) 32 + 9 * c / 5
axis.CF <-
function(side, ...)
{
ylim <- current.panel.limits()$ylim
switch(side,
left = {
prettyF <- pretty(ylim)
labF <- parse(text = sprintf("%s ~ degree * F", prettyF))
panel.axis(side = side, outside = TRUE,
at = prettyF, labels = labF)
},
right = {
prettyC <- pretty(F2C(ylim))
labC <- parse(text = sprintf("%s ~ degree * C", prettyC))
panel.axis(side = side, outside = TRUE,
at = C2F(prettyC), labels = labC)
},
axis.default(side = side, ...))
}
xyplot(nhtemp ~ time(nhtemp), aspect = "xy", type = "o",
scales = list(y = list(alternating = 3)),
axis = axis.CF, xlab = "Year", ylab = "Temperature",
main = "Yearly temperature in New Haven, CT")
## version using yscale.components
yscale.components.CF <-
function(...)
{
ans <- yscale.components.default(...)
ans$right <- ans$left
ans$left$labels$labels <-
parse(text = sprintf("%s ~ degree * F", ans$left$labels$at))
prettyC <- pretty(F2C(ans$num.limit))
ans$right$ticks$at <- C2F(prettyC)
ans$right$labels$at <- C2F(prettyC)
ans$right$labels$labels <-
parse(text = sprintf("%s ~ degree * C", prettyC))
ans
}
xyplot(nhtemp ~ time(nhtemp), aspect = "xy", type = "o",
scales = list(y = list(alternating = 3)),
yscale.components = yscale.components.CF,
xlab = "Year", ylab = "Temperature",
main = "Yearly temperature in New Haven, CT")
Run the code above in your browser using DataLab