开发者

change numeric tickmarks to letters on levelplot

开发者 https://www.devze.com 2023-03-10 17:40 出处:网络
I\'m working on on some details on my plots and I\'ve found one I can\'t get around. On my levelplot are 开发者_开发知识库numeric tickmarks. Now I want my tickmarks on the y-axis to be changed to the

I'm working on on some details on my plots and I've found one I can't get around. On my levelplot are 开发者_开发知识库numeric tickmarks. Now I want my tickmarks on the y-axis to be changed to the corresponding Letters. (i.e. 1=A, 5=E,27=AA, 29=AC,...)

I've already used

scales=list(
    y=list(alternating=3,labels=toupper(c(letters[1],letters[5],letters[10],letters[15])))))

But I want them to change depending on what's on the Y-axis, not from values I'm giving. Can anybody help me on this one?

levelplot(volcano,main="levelplot(volcano)")

change numeric tickmarks to letters on levelplot


Not general solution. Use pretty to compute at by self.

y_at <- pretty(seq_len(ncol(volcano)))
y_labels <- c(LETTERS, outer(LETTERS, LETTERS, paste, sep=""))
levelplot(
    volcano, main="levelplot(volcano)",
    scales=list(y=list(
        alternating=3,
        labels=y_labels[round(y_at)],
        at=y_at
    ))
)

change numeric tickmarks to letters on levelplot


Some discussion on chat indicates levelplot() handles the heatmap case (where one or both variables are discrete/categorical) just fine. Therefore, we just need to get the data into the correct format. One way to do that is to store at least the y coordinate as a factor with the appropriate levels set.

First, simulate some data along the lines of the OP's data:

yholes <- c(LETTERS, sort(outer(LETTERS, LETTERS, paste, sep="")))[1:30]
xholes <- seq_along(yholes)
g <- expand.grid(X = xholes, Y = yholes)
set.seed(2)
dat <- with(g, data.frame(Y = factor(Y, levels = yholes), 
                          X = X, 
                          vals = runif(900)))

which gives:

R> head(dat)
  Y X      vals
1 A 1 0.1848823
2 A 2 0.7023740
3 A 3 0.5733263
4 A 4 0.1680519
5 A 5 0.9438393
6 A 6 0.9434750

The reason for the expand.grid() call is that levelplot(), when used via the formula method, which I am about to make use of, needs a Y and a X for each data point (900 of them in this case).

The plot is then easy to produce via a simple formula in levelplot():

require(lattice)
levelplot(vals ~ X + Y, data = dat)

which produces this:

change numeric tickmarks to letters on levelplot

0

精彩评论

暂无评论...
验证码 换一张
取 消