I'm about to create a hexbin (lattice) plot that includes vertical lines and margin text. The margin text of the MWE should be located above the line and it should simply state the x-value of the line. Here's what I've got so far:

library(hexbin)  

test <- data.frame(VAR1 = rnorm(200), VAR2 = rnorm(200),
               GROUP = c(rep(-1.5,60), rep(0.4,20), rep(1.9,120)))

plot(hexbinplot(test$VAR1 ~ test$VAR2,
            panel = function(...){
              panel.hexbinplot(...)
              panel.abline(v = test$GROUP)},
            legend = list(top = 
                            list(fun = textGrob, 
                                 args = list(
                                   x = unit(test$GROUP/5, "native"),
                                   y = 0.5, 
                                   label = test$GROUP, 
                                   just = "center")))
))

As you can see from this example, I struggle finding out how to set the coordinates for the labels. Is there a way to use the "real" x-axis coordinates or do I have to somehow deal with the relative ones?

有帮助吗?

解决方案

Following the advice of @baptiste , I've created a custom axis ticks and labels on the top which worked out perfectly and made the code much nicer. Here it is:

axisG <- function(side, ...){
  if (side == "top"){
    at <- unique(test$GROUP)
    panel.axis(side = side, outside = TRUE, at = at, labels = at, rot = 0)
  }
  else axis.default(side = side, ...) 
}

plot(hexbinplot(test$VAR1 ~ test$VAR2,
                axis = axisG,
                panel = function(...){
                  panel.hexbinplot(...)
                  panel.abline(v = test$GROUP)}
))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top