【发布时间】:2017-08-10 12:32:08
【问题描述】:
我有这个闪亮的应用程序,我正在绘制几个dygraphs。不幸的是,我不知道会有多少情节。它可能会不时变化。所以我想出了使用uiOutput 和renderUI 来构建一个对地块数量做出反应的应用程序。见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")
})
})
})
这就引出了这个情节,在这里可以看到所有三个情节的图例都直接粘贴在一起了。我希望他们对各自的情节正确。
【问题讨论】: