John Doucette
John Doucette

Reputation: 4540

Making an Axis count _down_ in R

I'm plotting a rather strange wireframe. The y-axis should run from -50 to 0, but needs to be labeled with positive numbers. So the origin should be at (0,50,0), with y decreasing along the y-axis.

My first thought was to take the original command:

wireframe(z~x*(10*log10(y)), grid, colorkey=TRUE, drape=TRUE, scales=list(arrows=FALSE))

and just negate y in the formula:

wireframe(z~x*(-10*log10(y)), grid, colorkey=TRUE, drape=TRUE, scales=list(arrows=FALSE))

But wireframe is too clever, and flips the axises (and plotted data!) around so that the x-axis now ascends from 0 to 50.

How can I manually specific the tic labels on my y-axis?

EDIT: Here's the R code to produce this issue:

tf_model <- function(n,l){
 tf =  n*l
 return(tf)
}

n <- c(0:100)/100 * 0.1
l <- -c(0:100)/2
l <- 10^(l/10)

grid <- expand.grid(x=n, y=l)

grid$z <- tf_model(grid$x, grid$y)

library('lattice')
trellis.par.set("axis.line",list(col=NA,lty=1,lwd=1))

wireframe(z~x*(10*log10(y)), grid, colorkey=TRUE, drape=TRUE, scales=list(arrows=FALSE))

 wireframe(z~x*(-10*log10(y)), grid, colorkey=TRUE, drape=TRUE, scales=list(arrows=FALSE))

Upvotes: 3

Views: 1129

Answers (1)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162451

It sounds like what you really want is to change the labels that are attached to the tick marks of the y axis. You can do that by setting the labels component of the y component of a list passed to the scales argument:

wireframe(z~x*(10*log10(y)), grid, colorkey=TRUE, drape=TRUE, 
          scales = list(arrows = FALSE, y = list(labels = seq(0, 50, by = 10))))

## Or perhaps this -- I can't quite make out which you want.
wireframe(z~x*(10*log10(y)), grid, colorkey=TRUE, drape=TRUE, 
          scales = list(arrows = FALSE, y = list(labels = seq(50, 0, by = -10))))

In general, you can achieve fairly complete control over, say, the y-axis of a lattice plot by setting a combination of ylim and the at and labels components of the list passed to scales:

  • ylim: specifies the extent and orientation of the y-axis. For example, set ylim=c(0, 1000) to extend the y-axis, or ylim=c(0,-50) to reverse its orientation.

  • at: controls the location of tick marks on the axis. For example, scales = list(y=list(at=c(0,-50))) will only put tick marks at either end point of your default axes.

  • labels: set the labels to be placed at the tick marks (which are either set by the function's default behavior, or by the at argument). For example, scales = list(y=list(at=c(0,-50), labels=c("Zero", "Below Zero")))

Upvotes: 3

Related Questions