如何在 R 中使用 networkD3 添加轴标签
How to add Axis labels using networkD3 in R
我正在使用 networkD3 包制作桑基图,我在下面包含了我的代码。我想在图表的左侧和右侧添加标题、脚注和标签。但我不确定如何执行此操作,因为 networkD3 没有内置标签命令
library(dplyr)
library(networkD3)
library(htmlwidgets)
Comp_Noncomp<-c("A","B","C","A","B","C","A","B","C")
Category<-c("D","E","F","D","E","F","D","E","F")
Frequency<-c(1,2,3,4,5,6,7,8,9)
my.data.3<-data.frame(Comp_Noncomp,Category,Frequency)
nodes <- data.frame(name=c(as.character(my.data.3$Comp_Noncomp), as.character(my.data.3$Category)) %>% unique())
my.data.3$IDsource=match(my.data.3$Comp_Noncomp, nodes$name)-1
my.data.3$IDtarget=match(my.data.3$Category, nodes$name)-1
library(networkD3)
sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
您可以使用库 htmlwidgets
和 htmltools
向该图表添加内容。
例如,如果您想添加标题:
s <- sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
s2 <- htmlwidgets::prependContent(s, htmltools::tags$h1("This is my Title"))
假设您想添加样式和副标题。
s3 <- htmlwidgets::prependContent(s, htmltools::tags$h1("This is my Title",
style="text-align:center;"),
htmltools::tags$h2("This is my Subtitle",
style="color:#003b70; text-align:center;"))
你可以用它做很多事情,但你必须探索一下才能弄清楚你想要什么以及你想要它的地方。
根据您的评论更新
添加左右注释的最简单方法可能是使用库 manipulateWidget
。
您需要恢复到原始 Sankey 图,因为它会删除任何前置或附加的内容。
我将文本在左侧垂直居中。在右侧,我让系统选择位置。这样您就可以看到对齐内容的不同方式。
s <- sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
leftTx = tags$div(
style="max-width: 30vw; padding-bottom: 15px; height: 100%; display: flex; align-items: center; justify-content: center;",
tags$p("This text is on the left side"))
rightTx = tags$p("This text is on the right",
style="max-width:30vw")
cS <- combineWidgets(s,
title = tags$h1("This is my Title",
style="text-align:center;"),
leftCol = leftTx,
rightCol = rightTx,
nrow = 1)
我正在使用 networkD3 包制作桑基图,我在下面包含了我的代码。我想在图表的左侧和右侧添加标题、脚注和标签。但我不确定如何执行此操作,因为 networkD3 没有内置标签命令
library(dplyr)
library(networkD3)
library(htmlwidgets)
Comp_Noncomp<-c("A","B","C","A","B","C","A","B","C")
Category<-c("D","E","F","D","E","F","D","E","F")
Frequency<-c(1,2,3,4,5,6,7,8,9)
my.data.3<-data.frame(Comp_Noncomp,Category,Frequency)
nodes <- data.frame(name=c(as.character(my.data.3$Comp_Noncomp), as.character(my.data.3$Category)) %>% unique())
my.data.3$IDsource=match(my.data.3$Comp_Noncomp, nodes$name)-1
my.data.3$IDtarget=match(my.data.3$Category, nodes$name)-1
library(networkD3)
sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
您可以使用库 htmlwidgets
和 htmltools
向该图表添加内容。
例如,如果您想添加标题:
s <- sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
s2 <- htmlwidgets::prependContent(s, htmltools::tags$h1("This is my Title"))
假设您想添加样式和副标题。
s3 <- htmlwidgets::prependContent(s, htmltools::tags$h1("This is my Title",
style="text-align:center;"),
htmltools::tags$h2("This is my Subtitle",
style="color:#003b70; text-align:center;"))
你可以用它做很多事情,但你必须探索一下才能弄清楚你想要什么以及你想要它的地方。
根据您的评论更新
添加左右注释的最简单方法可能是使用库 manipulateWidget
。
您需要恢复到原始 Sankey 图,因为它会删除任何前置或附加的内容。
我将文本在左侧垂直居中。在右侧,我让系统选择位置。这样您就可以看到对齐内容的不同方式。
s <- sankeyNetwork(Links = my.data.3, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "Frequency", NodeID = "name",
sinksRight=FALSE)
leftTx = tags$div(
style="max-width: 30vw; padding-bottom: 15px; height: 100%; display: flex; align-items: center; justify-content: center;",
tags$p("This text is on the left side"))
rightTx = tags$p("This text is on the right",
style="max-width:30vw")
cS <- combineWidgets(s,
title = tags$h1("This is my Title",
style="text-align:center;"),
leftCol = leftTx,
rightCol = rightTx,
nrow = 1)