gpt4 book ai didi

r - 更改 networkD3 图的背景颜色

转载 作者:行者123 更新时间:2023-12-05 00:19:24 33 4
gpt4 key购买 nike

假设我制作了一个 networkD3 图 - 使用包中的最小示例

#
library(networkD3)

# Load data
data(MisLinks)
data(MisNodes)

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

如果我在浏览器中打开它,我可以使用开发人员工具将 body 的背景颜色更改为例如 background-color: #DAE3F9;"
有没有办法自动将绘图的背景颜色(从默认白色)定义为另一种颜色,而无需在浏览器中打开?基本上,我们可以像添加 JS 函数一样直接在代码中添加 CSS 吗?

最佳答案

如果可能,我们可以使用 htmltools 的帮助。以几种方式。

library(networkD3)

# Load data
data(MisLinks)
data(MisNodes)

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

library(htmltools)

# don't like using the !important modifier
# but without script not sure there is another way
# to override the default white background
browsable(
tagList(
tags$head(
tags$style('body{background-color: #DAE3F9 !important}')
),
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
)
)

# if you want to limit the background-color to
# the htmlwidget, we could wrap in tags$div
browsable(
tags$div(
style = "background-color:#DAE3F9;",
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8)
)
)

# if you are ok with script then we
# we could do something like this
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.backgroundColor = "#DAE3F9"
'
)
)
)

关于r - 更改 networkD3 图的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133746/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com