alberto
alberto

Reputation: 2645

absoluteGrob not found (ggplot2)

The function absoluteGrob {ggplot2} shows a behavior that I am not able to debug. I have ggplot2 installed and I can see the help page by ?absoluteGrob.

However, R does not find it when I try to use it:

> absoluteGrob
Error: object 'absoluteGrob' not found

More particularly, I try to execute the following code (from this answer to plot some graphs as x-labels):

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

But I get the error:

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

Any help (or alternatives) is welcome.

ggplot2 version:

ggplot2_1.0.1

Edit

Even in the simple case...

It does not work:

library(ggplot2)
absoluteGrob

It does:

library(ggplot2)
ggplot2:::absoluteGrob

Upvotes: 3

Views: 196

Answers (1)

Alex A.
Alex A.

Reputation: 5586

The answer you linked to in your post was posted 3 years ago as of this posting, and many things in ggplot2 have changed since then. At that point, ggplot2 version 0.9.0 had not yet been released.

According to the 1.0.0 documentation for absoluteGrob, it's still experimental, which means that it was certainly experimental at the time of the linked answer. At that point it was likely exported from the ggplot2 namespace and thus available to the user. That's why the linked answer worked at the time.

However, as of version 1.0.1, it is not exported from the ggplot2 namespace. So while you're able to view the source and documentation with ggplot2:::absoluteGrob (which works for non-exported objects) and ?absoluteGrob, you will not be able to use it, even by explicitly specifying the namespace via ggplot2::absoluteGrob.

According to the source, it simply calls gTree(), which is from the grid package, with cl="absoluteGrob". You can try that in place of calling absoluteGrob() directly. For example, try the following, which will hopefully mimic the desired behavior from 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)

Upvotes: 2

Related Questions