【问题标题】:How to embed links within a likert plot, separate links for each likert item如何在李克特图中嵌入链接,每个李克特项目的单独链接
【发布时间】:2016-03-28 20:41:54
【问题描述】:

我正在尝试使用 likert 包和 gridSVG 包的组合将超链接嵌入到 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)

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

【问题讨论】:

    标签: r svg ggplot2


    【解决方案1】:

    这种分组GROB 并不少见。由于我认为我们不想重写likert 来取消组合这些,因此我们最好通过在grid 之后使用XML 来操作SVG。这是实现此目的的一种方法。

    live example

    如果您希望此图形成为更大网页的一部分,我们还可以在 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 = ""
        )
      )
    )
    

    【讨论】:

    • timelyportfolio 的 javascript 示例似乎有效。谢谢!
    • 该示例似乎确实有效,但是如何导出带有链接轴标签的结果 svg 文件?在示例中,生成的 svg 在浏览器中打开,该浏览器似乎位于临时文件夹中,但我如何进行操作,然后使用链接轴导出生成的 svg(就像我之前导出“testPlot.svg “)?我尝试将文件名添加到示例代码的grid.export("", addClasses=TRUE)$svg 部分,它确实创建了一个 svg 文件,但它没有实时链接。感谢您的帮助!
    • 如果我理解正确,saveXML 会给你你想要的(见inside-r.org/packages/cran/xml/docs/saveXML)。如果您想要一个完整的 XML,您可以删除 prefix="",如果您想要一个文件,您可以指定 file = "mysvg.svg"saveXML(likert_svg, prefix = "" )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2016-06-16
    • 2021-11-09
    相关资源
    最近更新 更多