Adjust background picture and title for plot from networkD3's forceNetwork

故事扮演 提交于 2019-12-11 06:35:54

问题


Following the post here, Change the color of the legend text in forceNetwork for networkD3, I am trying to add a picture from a local drive as the background image, and also add a title to the graph.

However, these lines do not seem to take effect:

Background:    .style("background-image", "url(C:\\Desktop\\BGP.png)")
Title:          htmlwidgets::prependContent(htmltools::tags$h1("Title"))

What is the right way to add them in? Also, is there a way to adjust the font style and size of the title text as well?

library(networkD3)
library(htmlwidgets)

subNodes <- 
  read.table(stringsAsFactors = FALSE, header = TRUE, text = "
             nodeName nodeGroup     nodeSize
             Bob      NorthAmerica  10
             Alice    NorthAmerica  10
             Tom      China         10
             John     Japan         10
             ")

subLinkList <-
  read.table(stringsAsFactors = FALSE, header = TRUE, text = "
             root  children  linkValue
             0     1         1
             0     2         1
             0     3         1
             ")

linkJS <- JS('
  function(){
             d3.select("body")
             .style("background-image", "url(C:\\Desktop\\BGP.png)")
             .style("background-repeat", "no-repeat")
             .style("background-position", "right bottom")
             return 100;
             }')

network <- forceNetwork(Links = subLinkList, Nodes = subNodes,
               Source = "root", Target = "children",
               Value = "linkValue", NodeID = "nodeName",
               Group = "nodeGroup", 
               opacity = 1, Nodesize = "nodeSize",
               legend = TRUE, linkDistance = linkJS,
               colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20)"))

network1 <- htmlwidgets::onRender(
  network,
  'function(el, x) { 
  d3.selectAll(".legend text").style("fill", "white");
  d3.select("body").style("background-color", "#144370");
  }',
  htmlwidgets::prependContent(htmltools::tags$h1("Title"))
)


saveNetwork(network1, "c:\\forceNetwork.html", selfcontained = TRUE)

回答1:


Here's a reproducible example that adds a title, styles the title, styles the legend text, changes the background color, and attempts to set a background image with a local file. (I can't test the background image because it depends on a number of specific factors, but it may work for you.)...

library(networkD3)
library(htmlwidgets)

subNodes <- 
  read.table(stringsAsFactors = FALSE, header = TRUE, text = "
             nodeName nodeGroup     nodeSize
             Bob      NorthAmerica  10
             Alice    NorthAmerica  10
             Tom      China         10
             John     Japan         10
             ")

subLinkList <-
  read.table(stringsAsFactors = FALSE, header = TRUE, text = "
             root  children  linkValue
             0     1         1
             0     2         1
             0     3         1
             ")

network <- forceNetwork(Links = subLinkList, Nodes = subNodes,
                        Source = "root", Target = "children",
                        Value = "linkValue", NodeID = "nodeName",
                        Group = "nodeGroup", 
                        opacity = 1, Nodesize = "nodeSize",
                        legend = TRUE)

network <- htmlwidgets::prependContent(network, htmltools::tags$h1("Title"))

network <- htmlwidgets::onRender(
  network,
  'function(el, x) { 
    d3.selectAll(".legend text").style("fill", "white");
    d3.select("body").style("background-color", "#144370");
    d3.select("h1").style("color", "red").style("font-family", "sans-serif");
    d3.select("body")
      .style("background-image", "url(file://C:\\Desktop\\BGP.png)")
      .style("background-repeat", "no-repeat")
      .style("background-position", "right bottom");
  }'
)


saveNetwork(network, "~/Desktop/forceNetwork.html", selfcontained = TRUE)


来源:https://stackoverflow.com/questions/53828831/adjust-background-picture-and-title-for-plot-from-networkd3s-forcenetwork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!