1

使用visNetwork包,我们如何添加边框?

参考: GitHubCRAN在线文档

library(visNetwork)

nodes <- data.frame(id = 1:3, 
                    color.background = c("red", "blue", "green"),
                    color.highlight.background = c("red", NA, "red"), 
                    shadow.size = c(5, 10, 15))

edges <- data.frame(from = c(1,2), to = c(1,3),
                    label = LETTERS[1:2], 
                    font.color =c ("red", "blue"), 
                    font.size = c(10,20))

visNetwork(nodes, edges)

我们可以设置背景颜色,但我更喜欢情节的轮廓/边框。


如果它是相关的,我在shiny中使用它,这里是一个例子:

library(shiny)
library(visNetwork)

shinyApp(
  ui = fluidPage(
    visNetworkOutput("vis1", height = "200px"),
    visNetworkOutput("vis2", height = "200px")
  ),

  server = function(input, output, session) {
    nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
    edges <- data.frame(from = c(1,2), to = c(2,3))

    output$vis1 <- renderVisNetwork({
      visNetwork(nodes, edges,
                 main = "my vis 1")
      })
    output$vis2 <- renderVisNetwork({
      visNetwork(nodes, edges, background = "red",
                 main = "my vis 2, with red background")
    })

  })

并且想看剧情的轮廓,这里首先vis( vis1)没有轮廓,只是透明的背景,很难分辨出边框在哪里:

在此处输入图像描述

4

1 回答 1

2

您可以选择 CSS 解决方案,因为此上下文是特定的。

library(shiny)
library(visNetwork)

shinyApp(
  ui = fluidPage(

    tags$head(
      tags$style(HTML("
                      #vis1 {
                        border: 2px solid black;
                      }
                      "))
      ),

    visNetworkOutput("vis1", height = "200px"),
    visNetworkOutput("vis2", height = "200px")
  ),

  server = function(input, output, session) {
    nodes <- data.frame(id = 1:3, group = c("B", "A", "B"))
    edges <- data.frame(from = c(1,2), to = c(2,3))

    output$vis1 <- renderVisNetwork({
      visNetwork(nodes, edges,
                 main = "my vis 1")
    })
    output$vis2 <- renderVisNetwork({
      visNetwork(nodes, edges, background = "red",
                 main = "my vis 2, with red background")
    })

  })

我还建议将 CSS 放在自己的样式表中。请参阅本指南以了解如何执行此操作。

于 2018-03-07T23:49:06.030 回答