如何用超链接提供ggplot2图形? [英] How to furnish a ggplot2 figure with a hyperlink?

查看:149
本文介绍了如何用超链接提供ggplot2图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用超链接提供一个ggplot2图:

这个工作原理:

  library(gridSVG)
library(格子)

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

这不是:

  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)

这是什么?

解决方案

我已经问过gridSVG包的作者之一Simon Potter:
这是他的工作)回答:

我建议你在这里尝试开发版本:



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



它包含一个专门用于处理gTables(因此ggplot2图形)的解决方法。



因此,要尝试让您的示例工作,请启动新的R会话并运行以下代码:

  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)

这里唯一的区别是gridToSVG()的附加参数。这主要是为了减少输出到SVG文件和一个HTML包装器(否则你也会得到一些JSON数据,这对你的例子没有用)。


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?

解决方案

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).

这篇关于如何用超链接提供ggplot2图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆