Search code examples
rmatrixplotlatticelevelplot

How to Add Labels to Top X-Axis of Levelplot in Lattice Package


So I'm making correlation-matrix-like plots using levelplot similar to this (taken from: Plot correlation matrix into a graph):

library(lattice)

#Build the horizontal and vertical axis information
hor <- c("214", "215", "216", "224", "211", "212", "213", "223", "226", "225")
ver <- paste("DM1-", hor, sep="")

#Build the fake correlation matrix
nrowcol <- length(ver)
cor <- matrix(runif(nrowcol*nrowcol, min=0.4), nrow=nrowcol, ncol=nrowcol, dimnames = list(hor, ver))
for (i in 1:nrowcol) cor[i,i] = 1

#Build the plot
rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor, main="stage 12-14 array correlation matrix", xlab="", ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01))

I would like to add labels to each of the top (second) x-axis ticks. Does anyone know how to accomplish this?

Thank you,

Kai


Solution

  • You could do this by adding scales=list(alternating=3) to your call of levelplot

    If you look up the help for levelplot, for some information, you are referred to xyplot (and others). It is in the xyplot help page, you find a description of scales and therein alternating. Alternating controls the location of the tick labels and can take 4 values:

    • 0 (none),
    • 1 (bottom/left),
    • 2 (top/right),
    • 3 (both).

    Here is the call to levelplot that gives you ticks at all sides:

    levelplot(cor, main="stage 12-14 array correlation matrix", xlab="", ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01), scales=list(alternating=3))