在forceNetwork中更改背景图像

时间:2017-08-03 01:02:42

标签: r networkd3

更改networkD3的背景颜色可以通过劫持forceNetwork函数的一些参数来实现,如@Jota所示:Change background color of networkD3 plot

如何将其推断为更改背景图像?

我尝试了几种没有成功的方法,其中之一如下:

library(networkD3)
library(htmltools)

data(MisLinks)
data(MisNodes)

browsable(
  tagList(
         forceNetwork(Links = MisLinks, Nodes = MisNodes,
             Source = "source", Target = "target",
             Value = "value", NodeID = "name",
             Group = "group", opacity = 0.8),
      tags$script('document.body.style.backgroundImage = "url(paper.gif)"')
    )
   )

是否有办法以类似的方式改变背景图像:
 linkDistance = JS(' function(){d3.select(" body")。style(" background-color","#910e33");返回50;}'))?

1 个答案:

答案 0 :(得分:2)

您必须完全指定网址。例如,这似乎有用......

library(networkD3)
library(htmltools)

data(MisLinks)
data(MisNodes)

browsable(
  tagList(
    forceNetwork(Links = MisLinks, Nodes = MisNodes,
                 Source = "source", Target = "target",
                 Value = "value", NodeID = "name",
                 Group = "group", opacity = 0.8),
    tags$script('document.body.style.backgroundImage = "url(https://media.giphy.com/media/dgrEGTo4uk22Y/giphy.gif)"')
  )
)

这是另一种不使用htmltools ...

的方法
library(networkD3)

data(MisLinks)
data(MisNodes)

linkJS <- JS('
  function(){
    d3.select("body")
      .style("background-image", "url(https://media.giphy.com/media/dgrEGTo4uk22Y/giphy.gif)")
      .style("background-repeat", "no-repeat")
      .style("background-position", "right bottom");
    return 50;
  }')

forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", 
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 0.8, linkDistance = linkJS)