首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Shiny中打印sankey图

问在Shiny中打印sankey图
EN

Stack Overflow用户
提问于 2017-12-29 22:23:24
回答 1查看 3.4K关注 0票数 7

我创建了一个这样的sankey图:

代码语言:javascript
复制
#install.packages("networkD3")
library(networkD3)
nodes = data.frame("name" = 
                     c("Retour", # Node 0
                       "niet tevreden/ontevreden", # Node 1
                       "fout", # Node 2
                       "stuk",
                       "adres",
                       "verpakking",
                       "gebroken/glas"))# Node 3
links = as.data.frame(matrix(c(
  0, 1, 10, # Each row represents a link. The first number
  0, 2, 20, # represents the node being conntected from. 
  0, 3, 30,
  2, 4, 8,
  3, 5, 10,
  3, 6, 12# the second number represents the node connected to.
  ),# The third number is the value of the node
  byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "name",
              fontSize= 12, nodeWidth = 30)

工作很好,但是现在我想把这个图变成闪亮的。因此,他们做了以下工作:

代码语言:javascript
复制
## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      box(plotOutput("plot1", height = 250))
    )
  )
)

library(networkD3)
server <- function(input, output) {

  histdata <- rnorm(500)

  nodes = data.frame("name" = 
                       c("Retour", # Node 0
                         "niet tevreden/ontevreden", # Node 1
                         "fout", # Node 2
                         "stuk",
                         "adres",
                         "verpakking",
                         "gebroken/glas"))# Node 3
  links = as.data.frame(matrix(c(
    0, 1, 10, # Each row represents a link. The first number
    0, 2, 20, # represents the node being conntected from. 
    0, 3, 30,
    2, 4, 8,
    3, 5, 10,
    3, 6, 12# the second number represents the node connected to.
  ),# The third number is the value of the node
  byrow = TRUE, ncol = 3))
  names(links) = c("source", "target", "value")

  output$plot1 <- renderPlot({
    sankeyNetwork(Links = links, Nodes = nodes,
                  Source = "source", Target = "target",
                  Value = "value", NodeID = "name",
                  fontSize= 12, nodeWidth = 30)
  })
  output$plot2 <- renderPlot({
    data <- histdata[seq_len(10)]
    hist(data)
  })
}

shinyApp(ui, server)

事情是,因为某种原因,闪亮可以读懂桑基的图表。如果我更改代码:

代码语言:javascript
复制
box(plotOutput("plot1", height = 250))

至:

代码语言:javascript
复制
box(plotOutput("plot2", height = 250))

它正在绘制直方图。因此,sankey图似乎有问题。你对这是什么原因有什么想法?

EN

回答 1

Stack Overflow用户

发布于 2017-12-29 22:43:16

您应该在networkD3中使用renderSankeyNetwork和sankeyNetworkOutput函数,而不是plotOutput和renderPlot。因此,如果您的数据已经加载,它将看起来像...

代码语言:javascript
复制
library(shiny)
library(shinydashboard)
library(networkD3)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
      sankeyNetworkOutput("plot")
    )
  )
)

server <- function(input, output) {

  output$plot <- renderSankeyNetwork({
    sankeyNetwork(Links = links, Nodes = nodes,
                  Source = "source", Target = "target",
                  Value = "value", NodeID = "name",
                  fontSize= 12, nodeWidth = 30)
  })
}

shinyApp(ui, server)

另请参阅example here

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48024037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档