找不到 absoluteGrob (ggplot2)

absoluteGrob not found (ggplot2)

函数 absoluteGrob {ggplot2} 显示了我无法调试的行为。我安装了 ggplot2,我可以通过 ?absoluteGrob 查看帮助页面。

然而,当我尝试使用它时,R 并没有找到它:

> absoluteGrob
Error: object 'absoluteGrob' not found

更具体地说,我尝试执行以下代码(来自 this answer 以将一些图表绘制为 x 标签):

library(grid)
library(ggplot2)
library(grImport)
library(igraph)


npoints <- 3

y <- rexp(npoints)
x <- seq(npoints)

pics  <- vector(mode="list", length=npoints)
for(i in 1:npoints){
  fileps <- paste0("motif",i,".ps")
  filexml <- paste0("motif",i,".xml")

  # Postscript file
  postscript(file = fileps, fonts=c("serif", "Palatino"))
  plot(graph.ring(i), vertex.label.family="serif", edge.label.family="Palatino")
  dev.off()

  # Convert to xml accessible for symbolsGrob (my_axis)
  PostScriptTrace(fileps, filexml)
  pics[i] <- readPicture(filexml)
}


my_axis <- function () {
    function(label, x = 0.5, y = 0.5, ...) {
        absoluteGrob(
           do.call("gList", mapply(symbolsGrob, pics[label], x, y, SIMPLIFY = FALSE)),
           height = unit(1.5, "cm")
      )
    }
  }

qplot(factor(c("a", "b", "c")), 1:npoints) + scale_x_discrete(labels= my_axis())

但我收到错误消息:

Error in scale$labels(breaks) : could not find function "absoluteGrob" 

欢迎任何帮助(或替代方案)。

ggplot2 版本:

ggplot2_1.0.1

编辑

即使在简单的情况下...

无效:

library(ggplot2)
absoluteGrob

确实如此:

library(ggplot2)
ggplot2:::absoluteGrob

您在 post 中链接的答案是 3 年前 post 编辑的 posting,并且 ggplot2 中的许多内容从那时起都发生了变化.那时,ggplot2 版本 0.9.0 尚未发布。

根据 1.0.0 documentation for absoluteGrob,它仍然是实验性的,这意味着它在链接答案时肯定是实验性的。那时它可能是从 ggplot2 命名空间导出的,因此可供用户使用。这就是链接答案当时有效的原因。

但是,从 1.0.1 版开始,它不会从 ggplot2 命名空间中导出。因此,虽然您可以使用 ggplot2:::absoluteGrob(适用于非导出对象)和 ?absoluteGrob 查看源代码和文档,但您将无法使用它,即使通过显式指定命名空间也是如此ggplot2::absoluteGrob.

根据源代码,它只是调用 gTree(),它来自 grid 包,带有 cl="absoluteGrob"。您可以尝试用它代替直接调用 absoluteGrob()。例如,尝试以下操作,这将有望模仿 absoluteGrob():

中的所需行为
grlist <- do.call("gList", mapply(symbolsGrob, pics[label], x, y, SIMPLIFY = FALSE))

gTree(children = grlist,
      cl = "absoluteGrob",
      height = unit(1.5, "cm"),
      width = NULL,
      xmin = NULL,
      ymin = NULL,
      vp = NULL)