【发布时间】:2021-01-21 22:24:56
【问题描述】:
我需要创建一个由子图组成的闪亮图,该图将使用不断变化的行数动态创建(但每行只有 2 个图)。我希望每个子图的大小相同,并在所有子图之间留出一些空间,以便没有重叠,并将图例居中放置在整个图上方。
但是,当添加更多的子图行时,图例和子图之间以及子图行之间的间距会不正常。我添加的行越多,它看起来越糟糕。当行数是动态的时,是否有一些技巧可以使子图看起来漂亮和标准化?
我在下面插入了一个闪亮的示例应用程序,您可以在其中更改图例的行数、子图边距和垂直位置。这是我的情节的基本格式 - 有人对如何修复和标准化子情节和图例的排列方式有任何建议吗?
library(shiny)
library(plotly)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
width = 2,
numericInput(inputId = "left", label = "left", value = 0.05, min = 0),
numericInput(inputId = "right", label = "right", value = 0.05, min = 0),
numericInput(inputId = "top", label = "top", value = 0.05, min = 0),
numericInput(inputId = "bottom", label = "bottom", value = 0.05, min = 0),
numericInput(inputId = "legend", label = "legend", value = 1.2, min = 0),
numericInput(inputId = "rows", label = "rows", value = 1, min = 1)
),
mainPanel(
width = 10,
plotlyOutput("plot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot <- renderPlotly({
rows <- input$rows
plot <- plot_ly(height = 400*rows)
plot <- add_trace(plot, data = economics, x = ~date, y = ~uempmed)
plot <- layout(
plot,
annotations = list(text = HTML("<b>title</b>"), showarrow = FALSE,
xref = 'x', x = 0.5, yref ='paper', y = 1.1),
legend = list(orientation = "h",
xanchor = "center", x = 0.5,
yanchor = "center", y = input$legend)
)
plots <- c()
for (x in 1:(rows*2)) {
plots[[x]] <- plot
}
subplot(plots, nrows = rows, shareY = FALSE, shareX = FALSE, titleY = TRUE, titleX = FALSE,
margin = c(input$left, input$right, input$top, input$bottom))
})
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: