Search code examples
rggplot2gridextrar-grid

How to have a clickable picture in a PDF created using pictureGrob?


I create a simple plot and have a picture actually an SVG icon as follows:

library(ggplot2); library(grid); library(gridExtra)

facebookGrob <- gTree(children=gList(pictureGrob(readPicture("inst/svg/facebook2.svg"))))

p1 <- ggplot() + 
  ggplot2::annotation_custom(facebookGrob, xmin=1.8, xmax=3.2, ymin=-0.6, ymax=1)

final <- arrangeGrob(p1,...,)
ggsave(filename='output.pdf',plot=final,...)

Is there any way to generate a clickable link on top of this SVG icon in the final PDF?


Solution

  • the tikzDevice package lets you insert hyperref links as nodes,

    library(tikzDevice)
    tikz("annotation.tex",width=4,height=4, standAlone = TRUE,
         packages = c(getOption('tikzLatexPackages'),
                      "\\usepackage{hyperref}",
                      "\\usetikzlibrary{positioning}")
    )
    
    tg <- tikzNodeGrob(x = 0.5, y = 0.5, name = 'google',
                 content='\\href{http://www.google.com}{\\includegraphics[width=1in]{google.png}}',
                 units = "native")
    
    qplot(1:10, 1:10) +
      annotation_custom(grob = tg, xmin=3,xmax=3,ymin=5,ymax=5)
    
    dev.off()
    

    enter image description here