2

I would like to produce a contourplot using the lattice package in which the y axis starts at the top and goes to the bottom (i.e. smallest value at top, largest value at bottom). If I change the following:

contourplot(Male ~ Age * Year, data=this.ds)

to

contourplot(Male ~ Age * rev(Year), data=this.ds)

Then the graph is plotted correctly but the y axis tickmark labels are not reversed. (i.e. still start at the bottom and go to the top.) - (See end of message for fully reproducible example.)

I think the answer involves using the 'scales' list object (so I think this has a one line solution) but am not sure what it is.

Help much appreciated, Jon

Fully reproducible example:

library(lattice)
attach(environmental)
ozo.m <- loess((ozone^(1/3)) ~ wind * temperature * radiation,
           parametric = c("radiation", "wind"), span = 1, degree = 2)
w.marginal <- seq(min(wind), max(wind), length.out = 50)
t.marginal <- seq(min(temperature), max(temperature), length.out = 50)
r.marginal <- seq(min(radiation), max(radiation), length.out = 4) 
wtr.marginal <- list(wind = w.marginal, temperature = t.marginal,
                              radiation = r.marginal) 
grid <- expand.grid(wtr.marginal) 
grid[, "fit"] <- c(predict(ozo.m, grid))

Plotting as usual:

contourplot(fit ~ wind * temperature, data = grid,
              cuts = 10, region = TRUE,
              xlab = "Wind Speed (mph)",
              ylab = "Temperature (F)",
              main = "Cube Root Ozone (cube root ppb)")

Using the rev() function on temperature:

contourplot(fit ~ wind * rev(temperature), data = grid,
            cuts = 10, region = TRUE,
            xlab = "Wind Speed (mph)",
            ylab = "Temperature (F)",
            main = "Cube Root Ozone (cube root ppb)")

detach()

I would like it so the y (temperature) axis labels are reversed as well as as the y axis values. (i.e. it reads 90, 80, 70, 60 going bottom to top rather than 60, 70, 80, 90)

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
JonMinton
  • 1,239
  • 2
  • 8
  • 26
  • Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and [this](http://meta.stackoverflow.com/help/how-to-ask). – Henrik Sep 07 '13 at 18:06
  • Thank you. I've added a fully reproducible example to try to make this clearer. – JonMinton Sep 07 '13 at 18:25
  • Thanks for a nice example! One tiny thing, insert a line break between these two commands: `grid <- expand.grid(wtr.marginal) grid[, "fit"] <- c(predict(ozo.m, grid))` (I wasn't allowed to do this edit). Then it will be very easy for anyone that wants help you just to copy-paste your code and play around with it. Thanks! – Henrik Sep 07 '13 at 18:41

2 Answers2

2

A cleaner solution is to reverse the order of the length-two vector that specifies the y-axis limits. Setting ylim=rev(range(temperature)) works well enough, though for a plot that more precisely matches the layout of the original, you may want to use extendrange() to plot a slightly larger extent.

## OP's example figure
a <- contourplot(fit ~ wind * temperature, data = grid,
                 cuts = 10, region = TRUE,
                 xlab = "Wind Speed (mph)",
                 ylab = "Temperature (F)",
                 main = "Cube Root Ozone (cube root ppb)")

## Same figure with the y-axis reversed 
b <- update(a, ylim = rev(extendrange(temperature, f=0.01)))

## Plot figs side-by-side to check that that worked
library(gridExtra)
grid.arrange(a, b, ncol=2)

(Although I used update() in the above to add the modified ylim, one could just as well supply it in the original call to contourplot. I only did it this way since I'm wanting to directly compare the original and modified plots.)

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
1

You may try to reverse the labels of the y axis in the scales argument.

library(lattice)
contourplot(fit ~ wind * rev(temperature), data = grid,
            cuts = 10, region = TRUE,
            xlab = "Wind Speed (mph)",
            ylab = "Temperature (F)",
            main = "Cube Root Ozone (cube root ppb)",
            scales = list(y = list(
              labels = seq(from = 100, to = 60, by = -10))))

To be honest I thought seq(from = 90, to = 60, by = -10) should work, but it gave labels 80, 70, 60, NA.

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Thanks. I ended up doing almost the same thing, although using the update command instead. It feels like a dirty hack, but works well enough for now. – JonMinton Sep 07 '13 at 20:18