R中网格图形的边缘坐标

时间:2014-01-14 15:01:19

标签: r plot lattice

我即将创建一个包含垂直线和边距文字的hexbinlattice)图。 MWE的边缘文本应该位于线的上方,它应该简单地说明线的x值。这是我到目前为止所得到的:

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")))
))

从这个例子中可以看出,我很难找到如何设置标签的坐标。有没有办法使用“真正的”x轴坐标,还是我必须以某种方式处理相关的坐标?

1 个答案:

答案 0 :(得分:1)

遵循@baptiste的建议,我在顶部创建了一个自定义轴刻度和标签,这些标签和标签完美地完成,使代码更好。这是:

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)}
))