Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在likert图中嵌入链接,请为每个likert项单独设置链接_R_Svg_Ggplot2 - Fatal编程技术网

如何在likert图中嵌入链接,请为每个likert项单独设置链接

如何在likert图中嵌入链接,请为每个likert项单独设置链接,r,svg,ggplot2,R,Svg,Ggplot2,我正在尝试使用likert包和gridSVG包的组合,在likert数据的绘图中嵌入超链接。我想将每个问题的文本链接到一个单独的链接,但我遇到了问题。下面的代码在每个问题的文本中嵌入了一个链接,但我不知道如何分别嵌入每个问题,因为这组问题似乎被分组在一个单独的组中。提前感谢您的投入 #creates an example plot from sample data from likert package. require(likert) data(pisaitems) items29 <

我正在尝试使用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")
#根据likert软件包中的样本数据创建示例图。
要求(利克特)
数据(pisaitems)

项目29这种分组的
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 = ""
    )
  )
)
#根据likert软件包中的样本数据创建示例图。
要求(利克特)
数据(pisaitems)

items29 timelyportfolio的javascript示例似乎有效。谢谢该示例看起来确实有效,但如何导出带有链接轴标签的结果svg文件?在本例中,生成的svg将在浏览器中打开,该浏览器似乎位于临时文件夹中,但如何进行操作,然后使用链接轴导出生成的svg(就像我以前导出“testPlot.svg”时所做的那样)?我尝试将文件名添加到示例代码的
grid.export(“,addClasses=TRUE)$svg
部分,该部分代码确实创建了一个svg文件,但没有实时链接。谢谢你的帮助!如果我理解正确,
saveXML
将为您提供所需内容(请参阅)。如果需要完整的XML,可以删除
前缀=“
,如果需要文件,可以指定
file=“mysvg.svg”
<代码>保存XML(likert_svg,前缀=”)