我试图使用likert包和gridSVG包的组合,将超链接嵌入到likert数据图中。我想把每个问题的文本链接到一个单独的链接上,但是我遇到了一些问题。下面的代码嵌入到每个问题的文本的单个链接,但我无法找到如何分别嵌入每个问题,因为这组问题似乎被分组在一个单一的grob中。提前感谢您的投入。
#creates an example plot from sample data from likert package.
require(likert)
data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == "ST25Q" ]
names(items29) <- c("Magazines", "Comic books", "Fiction",
"Non-fiction books", "Newspapers")
l29 <- likert(items29)
summary(l29)
plot(l29)
require(grid)
require(gridSVG)
#identifies grob of question text (all questions are in a single grob)
titleGrobName <- grep("axis-l.3-3-3-3", grid.ls(print=FALSE)$name, value=TRUE)
#embeds link in grob
grid.hyperlink(titleGrobName, "http://www.r-project.org")
#creates svg
gridToSVG("testPlot.svg", "none", "none")发布于 2016-03-28 12:02:22
这种分组GROB并不少见。因为我认为我们不想重写likert来取消对它们的分组,所以我们最好用XML在grid之后操作SVG。这里有一种方法可以实现这一点。
实例化
我们也可以添加在HTML/JavaScript方面的链接,如果你期望这张图形是一个更大的网页的一部分。
#creates an example plot from sample data from likert package.
require(likert)
data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == "ST25Q" ]
names(items29) <- c("Magazines", "Comic books", "Fiction",
"Non-fiction books", "Newspapers")
l29 <- likert(items29)
summary(l29)
plot(l29)
# if possible to use htmltools from RStudio
# install.packages("htmltools")
# then we can add the links on the
# XML side instead of in grid
library(XML)
library(htmltools)
library(gridSVG)
# export as XML SVG
likert_svg <- grid.export("", addClasses=TRUE)$svg
# find our axes
nodes <- getNodeSet(
likert_svg,
# thanks http://stackoverflow.com/questions/5818681/xpath-how-to-select-node-with-some-attribute-by-index
"(//x:g[contains(@id,'axis')])[1]//x:tspan",
"x"
)
lapply(
nodes,
function(node){
# get the text value of the node
lbl = xmlValue(node)
# remove the text from our node
xmlValue(node) <- ""
# create a <a href=> hyperlink
# https://www.w3.org/wiki/SVG_Links
a_node <- newXMLNode(
"a",
####### change your link here ###########
attrs = c("xlink:href"=paste0("http://google.com/search?q=",lbl)),
lbl
)
# add our new linked text to the node
addChildren(node, a_node)
}
)
# look at it in the browser/RStudio Viewer
browsable(
HTML(
saveXML(
# export as SVG XML
likert_svg,
prefix = ""
)
)
)https://stackoverflow.com/questions/36222104
复制相似问题