【问题标题】:dygraphs legend outside plot in shiny app for multiple plotsdygraphs legend在闪亮的应用程序中的情节之外,用于多个情节
【发布时间】:2017-08-10 12:32:08
【问题描述】:

我有这个闪亮的应用程序,我正在绘制几个dygraphs。不幸的是,我不知道会有多少情节。它可能会不时变化。所以我想出了使用uiOutputrenderUI 来构建一个对地块数量做出反应的应用程序。见https://shiny.rstudio.com/articles/dynamic-ui.html

现在我想在各自的绘图之外显示每个 dygraph 的图例,如下所示:Is there a way to add legend next to a dygraph in R, not exactly on the plot?

我现在的问题是图例的 <div> 元素的高度与图中的元素的高度不同。

我的代码是:

用户界面:

library(dygraphs)
shinyUI(fluidPage(
 titlePanel("Simple example"),
 sidebarLayout(
  sidebarPanel(),
  mainPanel(
   fluidRow(column(10, uiOutput("graphs")),
            column(2, uiOutput("legends")))
   )
 )
))

服务器:

library(dygraphs)
library(xts)

shinyServer(function(input, output, session) {

# load xts sample data
data("sample_matrix")
sample.xts <- as.xts(sample_matrix)

output$graphs <- renderUI({ 
 plot_output_list <- lapply(1:3, function(i) { 
  dygraphOutput(paste0('div_graph_', i)) 
 }) 
})

output$legends <- renderUI({
 legend_output_list <- lapply(1:3, function(i) {
   htmlOutput(paste0("div_legende",i), height = "400px")
 })
})

# do the plotting
lapply(1:3, function(i) {
  output[[paste0('div_graph_', i)]] <- renderDygraph({
   dygraph(sample.xts[,i],main=i)%>%
    dyLegend(labelsDiv = paste0("div_legende",i), show = "always")
  })
 })
})

这就引出了这个情节,在这里可以看到所有三个情节的图例都直接粘贴在一起了。我希望他们对各自的情节正确。

【问题讨论】:

    标签: r shiny dygraphs


    【解决方案1】:

    我明白了。 创建一个plotOutput 和一个空的plot 就可以了:

    Ui 保持不变。 服务器:

    library(dygraphs)
    library(xts)
    
    shinyServer(function(input, output, session) {
    
     data("sample_matrix")
     sample.xts <- as.xts(sample_matrix)
    
     output$graphs <- renderUI({ 
      plot_output_list <- lapply(1:3, function(i) { 
      dygraphOutput(paste0('div_graph_', i)) 
     }) 
    })
    
    output$legends <- renderUI({
     legend_output_list <- lapply(1:3, function(i) {
      plotOutput(paste0("div_legende",i), height = "400px")
     })
    })
    
    lapply(1:3, function(i) {
      output[[paste("div_legende",i)]] <- renderPlot(
        plot(1,1,type="n",xaxt="n",yaxt="n",ylab="",xlab="",bty="n"),
        height = "400px"
      )
      output[[paste0('div_graph_', i)]] <- renderDygraph({
      dygraph(sample.xts[,i],main=i)%>%
        dyLegend(labelsDiv = paste0("div_legende",i),
                 show = "always")
      })
     })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-25
      • 2021-05-10
      • 2018-01-31
      • 2017-11-07
      • 2021-12-18
      • 2017-06-14
      • 2017-08-12
      • 1970-01-01
      相关资源
      最近更新 更多