12

I save a plot generated with R with the pdf() function (see below). Is it possible, to add clickable hyperlinks to this plot? Alternatives to pdf() are welcome.

pdf(file="plot.pdf",width=20,height=50)
q <- ggplot(df, aes(x=reorder(desc,Value, FUN=median), y=Value))
q + geom_boxplot(aes(fill = factor(role)))+ coord_flip()
dev.off()

where the df$desc looks like this:

[1] "http://www.jcvi.org/cgi-bin/tigrfams/HmmReportPage.cgi?acc=TIGR02914  #  EpsI_fam: EpsI family protein  # Role: 141"                                        
[2] "http://www.jcvi.org/cgi-bin/tigrfams/HmmReportPage.cgi?acc=TIGR03067  #  Planc_TIGR03067: Planctomycetes uncharacterized domain TIGR03067  # Role: 157"     
[3] "http://www.jcvi.org/cgi-bin/tigrfams/HmmReportPage.cgi?acc=TIGR03021  #  pilP_fam: type IV pilus biogenesis protein PilP  # Role: 91"   

In the pdf, the link is not clickable.

Atticus
  • 783
  • 2
  • 6
  • 13
  • 1
    Have you seen any documentation anywhere that indicates that this might be possible? I'd look into perhaps a graphic format like SVG, but I'm not sure how much of this you're going to be able to do from within R. – A5C1D2H2I1M1N2O1R2T1 Dec 08 '13 at 10:15
  • 1
    Would be a nice little feature request though. Surely there exists a package somewhere that does this... (I don't know of one). – Simon O'Hanlon Dec 08 '13 at 10:26
  • @ Ananda Mahto: No, I dont. If this is possible with SVG output, I will use this. – Atticus Dec 08 '13 at 11:14
  • 1
    [Here](http://stackoverflow.com/questions/19414763/detect-and-alter-strings-in-pdfs/19551997#19551997) I add highlights for arbitrary text. Minimal changes (and easier code) to add `Link` annotation instead of `Highlight` annotation. – user2846289 Dec 08 '13 at 11:20
  • 2
    One option is to use latex to create the link (use latex package hyperref), but that would make the whole image a link. You could also try the (archived) tikzDevice package to do something similar. – Thomas Dec 08 '13 at 11:21
  • 1
    for future reference: http://stackoverflow.com/questions/4691780/create-pdf-with-tooltips-in-r/ pointed me to ways to get a `tikzDevice` to generate a PDF from R and it's pretty easy to put `\href{url}{text}` in instead of the tooltip text. will write up a proper answer when I get there! – Sam Mason Jan 14 '14 at 11:45

1 Answers1

3

You could do this with Rsweave. Rsweave lets you call R from within LaTeX.

So an example file using my own made up data would be:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\SweaveOpts{concordance=TRUE}


<<echo=FALSE,fig=TRUE>>=
    library(ggplot2)
    q <- ggplot() + geom_point(data=data.frame(x = c(1,2,3,4),y=c(4,3,2,1)), aes(x=x,y=y))
    print(q)
@
\par{
    \url{http://google.com}
}

\end{document}

And you can compile this from Rstudio. It will know what to do if the file has an .rnw extension. If you are compiling from R then you can use the Sweave command.

wheaton
  • 87
  • 4