Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to furnish a ggplot2 figure with a hyperlink?

Tags:

r

ggplot2

svg

I am trying to furnish a ggplot2 plot with a hyperlink:

This works:

library(gridSVG)
library(lattice)

xyplot(mpg~wt, data=mtcars, main = "Link to R-project home")
mainGrobName <- grep("main", grid.ls()[[1]], value=TRUE)
grid.hyperlink(mainGrobName, "http://www.r-project.org")
gridToSVG("HyperlinkExample.svg")

This not:

p = ggplot(mtcars, aes(wt, mpg)) + geom_point()+ labs(title="link")
print(p)
mainGrobName <- grep("title", grid.ls()[[1]], value=TRUE)
grid.hyperlink(mainGrobName, "http://www.r-project.org")
gridToSVG("HyperlinkExample.svg")

Any hints on this?

like image 545
steffi Avatar asked Dec 28 '12 14:12

steffi


1 Answers

I have asked Simon Potter, one of the authors of the gridSVG package: Here is his (working) answer:

I suggest you try the development version here:

http://r-forge.r-project.org/R/?group_id=1025

It contains a workaround specifically to deal with gTables (and therefore ggplot2 graphics).

So to try and get your example to work, start up a new R session and run the following code:

install.packages("gridSVG", repos="http://R-Forge.R-project.org")
library(gridSVG)
library(ggplot2)
(p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(title="link"))
titleGrobName <- grep("title", grid.ls(print=FALSE)$name, value=TRUE)
grid.hyperlink(titleGrobName, "http://www.r-project.org/")
gridToSVG("HyperlinkExample.svg", "none", "none")

The only real difference here are the additional parameters given to gridToSVG(). This is mainly to reduce the output to just the SVG file and an HTML wrapper (otherwise you also get some JSON data, which is not useful for your example).

like image 65
steffi Avatar answered Oct 13 '22 04:10

steffi