首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将链接嵌入到likert图中,为每个likert项单独链接

如何将链接嵌入到likert图中,为每个likert项单独链接
EN

Stack Overflow用户
提问于 2016-03-25 14:42:38
回答 1查看 157关注 0票数 1

我试图使用likert包和gridSVG包的组合,将超链接嵌入到likert数据图中。我想把每个问题的文本链接到一个单独的链接上,但是我遇到了一些问题。下面的代码嵌入到每个问题的文本的单个链接,但我无法找到如何分别嵌入每个问题,因为这组问题似乎被分组在一个单一的grob中。提前感谢您的投入。

代码语言: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)

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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-28 12:02:22

这种分组GROB并不少见。因为我认为我们不想重写likert来取消对它们的分组,所以我们最好用XMLgrid之后操作SVG。这里有一种方法可以实现这一点。

实例化

我们也可以添加在HTML/JavaScript方面的链接,如果你期望这张图形是一个更大的网页的一部分。

代码语言: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 = ""
    )
  )
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36222104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档