[R] insert hyperlink into svg graphic

Paul Murrell p@u| @end|ng |rom @t@t@@uck|@nd@@c@nz
Tue Mar 7 22:12:10 CET 2023


Hi

I think the main issue here is that you are *drawing* text on the 
graphics device, so I would only expect to see literal XML text output 
in the result.

If you want to affect the SVG code itself, you need a graphics device 
that understands the concept of hyperlinks.  One way to do that is with 
the 'gridSVG' package.  For example ...

library(ggplot2)
ggplot(mtcars) +
     geom_point(aes(disp, mpg)) +
     ggtitle('Fuel Efficiency')
## Make grobs accessible
library(grid)
grid.force()
## Find the name of the title grob
## grid.ls()
## Add hyperlink
library(gridSVG)
grid.hyperlink("title.3-5-3-5",
                href="https://en.wikipedia.org/wiki/Fuel_efficiency")
## Export to SVG
grid.export("link.svg")


It looks like you are using "base" graphics for your plot, in which case 
the template is slightly different, requiring the 'gridGraphics' package 
as well to convert to 'grid' output, for example ...


plot(mpg ~ disp, mtcars, main="Fuel Efficiency")
## Convert to 'grid' version
library(gridGraphics)
grid.echo()
## Find the name of the title grob
## grid.ls()
## Add hyperlink
library(gridSVG)
grid.hyperlink("graphics-plot-1-main-1",
                href="https://en.wikipedia.org/wiki/Fuel_efficiency")
## Export to SVG
grid.export("link-base.svg")


Hope that helps

Paul


On 8/03/23 08:09, Boris Steipe wrote:
> This was actually the first thing that ChatGPT debugged for me.
> 
> The issue was that I was able to click on the link when I displayed the 
> raw SVG in the browser (you can use that to test whether the syntax is 
> even correct), but not when the svg displays inside a html page with the 
> <img ...> tag.
> 
> ChatGPT correctly identified the issue and suggested a solution using 
> <object ...> tags instead.
> 
> Here's how that would work:
> 
> <object data="temps.svg" type="image/svg+xml" alt="Image description TK">
> <p>Your browser does not support the <object> tag.</p>
> </object>
> 
> (Most browsers support the object tag: see 
> https://caniuse.com/mdn-html_elements_object 
> <https://caniuse.com/mdn-html_elements_object> )
> 
> 
> Cheers,
> Boris
> 
> 
> 
>  > On 2023-03-07, at 10:33, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>  >
>  > Perhaps this thread on stackoverflow (from a search on "include hyperlink
>  > in svg graphic") may be of use:
>  >
>  > https://stackoverflow.com/questions/41970535/a-hyperlink-in-svg-use 
> <https://stackoverflow.com/questions/41970535/a-hyperlink-in-svg-use>
>  >
>  > Cheers,
>  > Bert
>  >
>  > On Tue, Mar 7, 2023 at 12:34 AM Rusty Travis <rusytravis19 using gmail.com> 
> wrote:
>  >
>  >> Greetings,
>  >>
>  >> I am creating plots of weather data and wish to include a link to the
>  >> data source into a title() object in the plot. My syntax must be wrong
>  >> though, as the hyperlink doesn't display, just the text and tags.
>  >>
>  >>> svg("/tmp/temps.svg", width=9, height=6)
>  >>> #plot created here
>  >>> title(sub="produced by Rusty Travis \nusing data from <a
>  >> xlink:href=https://www.ncei.noaa.gov/cdo-web/ 
> <https://www.ncei.noaa.gov/cdo-web>>test</a> ", line=-1.5,
>  >> adj=1)
>  >>> dev.off()
>  >>
>  >> The goal is to create a hyperlink of the word "test" in the above
>  >> example. What is the correct syntax, please?
>  >>
>  >> [[alternative HTML version deleted]]
>  >>
>  >> ______________________________________________
>  >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>  >> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>  >> PLEASE do read the posting guide
>  >> http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>
>  >> and provide commented, minimal, self-contained, reproducible code.
>  >>
>  >
>  > [[alternative HTML version deleted]]
>  >
>  > ______________________________________________
>  > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>  > https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>  > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>
>  > and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland 1142, New Zealand
64 9 3737599 x85392
paul using stat.auckland.ac.nz
www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list