AbsoluteGrob not found (ggplot2)

The function absoluteGrob {ggplot2}

shows behavior that I am unable to debug. I have it installed ggplot2

and I can see the help page ?absoluteGrob

.

However, R doesn't find it when I try to use it:

> absoluteGrob
Error: object 'absoluteGrob' not found

      

Specifically, I am trying to execute the following code (from this answer to draw some graphs as x-marks):

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 am getting error:

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

      

Any help (or alternatives) is greatly appreciated.

ggplot2 version:

ggplot2_1.0.1

Edit

Even in a simple case ...

Does not work:

library(ggplot2)
absoluteGrob

      

He does:

library(ggplot2)
ggplot2:::absoluteGrob

      

+3


source to share


1 answer


The answer you linked to in your post was posted 3 years ago from this post and a lot of things ggplot2

have changed since then. At this point, ggplot2

version 0.9.0 has not yet been released.

According to the 1.0.0 documentation forabsoluteGrob

, it's still experimental, which means it was definitely experimental at the time of the linked answer. At this point, it was most likely exported from the namespace ggplot2

and therefore available to the user. This is why the linked answer worked at the time.

However, since version 1.0.1 it is not exported from the namespace ggplot2

. So when you can view the source and documentation with ggplot2:::absoluteGrob

(which works for non-exported objects) and ?absoluteGrob

, you won't be able to use it even if you explicitly specify the namespace with ggplot2::absoluteGrob

.



According to the source, it just calls gTree()

which one from the package grid

, s cl="absoluteGrob"

. You can try this instead of calling directly absoluteGrob()

. For example, try the following, which hopefully mimics 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)

      

+2


source







All Articles