Built with blockbuilder.org
Answer to StackOverflow How to embed links within a likert
#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 = ""
)
)
)