On 2012-07-13 01:05, Martin Ivanov wrote:
  Dear R users,

I need to add minor axis ticks to my graph. In traditional R this is easily 
achievable by simply
adding a second axis with the minor ticks. But how to do that in trellis? I am 
already out of ideas.

Any suggestions will be appreciated.

Haven't seen a response yet, so I'll give it a shot,
sure to be replaced by something much simpler by
Deepayan when he finds the time.

Here are two ways:

1.
Assign appropriate values to the elements of
the xscale.components list. I prefer this.

## make some data
d <- data.frame(x = 1:12, y = rnorm(12))
at.ticks <- c(4,8)
at.labels <- c(2,6,10)
the_labels <- letters[1:3]

library(lattice)

## define a function to modify the xscale components;
## this function will be used inside xyplot().
myxscale.components <- function(...)
{
    ans <- xscale.components.default(...)
    ans$bottom$ticks$at <- at.ticks
    ans$bottom$labels$at <- at.labels
    ans$bottom$labels$labels <- the_labels
    ans
}

## do the plot
xyplot(y ~ x, data = d,
    scales = list(tck = c(1,0)),
    xscale.components = myxscale.components)

You can put the modifying function inside the xyplot call.
See ?axis.components.


2.
This is more like the base graphics way.
We create the plot without the x-axis and then
use the trellis.focus/unfocus functions in
conjunction with the panel.axis() function.
See ?panel.axis for details.

Here's the function to apply after the xyplot call:

myfocus <- function(){
  trellis.focus("panel", 1, 1,
     clip.off = TRUE,
     highlight = FALSE)

  ## put the ticks in
  panel.axis(side = "bottom",
     at = at.ticks,
     labels = FALSE,
     ticks = TRUE,
     tck = 1, outside = TRUE
  )

  ## put the labels in
  panel.axis(side = "bottom",
     at = at.labels,
     labels = the_labels,
     ticks = FALSE,
     tck = 0, outside = TRUE,
     rot = 0   # optional; try it without
  )
  trellis.unfocus()
}


xyplot(y ~ x, data = d,
    scales = list(
         y = list(tck = c(1,0)),
         x = list(tck = c(0,0),
                at = 1, label = ""  # to give us some bottom space
             )))

## Now add the axis ticks and labels
myfocus()


Peter Ehlers



Best regards,

Martin

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to