【问题标题】:Arranging plotly subplots nicely in shiny以闪亮的方式很好地安排情节子图
【发布时间】: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)

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    解决您的问题的一种方法是将图例位置、高度和边距定义为取决于行数。试试这个

    server <- function(input, output) {
      
      output$plot <- renderPlotly({
        
        rows <- input$rows
        mylegend <- 1 + 1/(5*rows)
        myht <- c()
        if (rows==1) {
          myht <- c(1)
          rowsd <- 1
        }else {
          rowsd <- rows-1
          myht <- 1/rows
          for (i in 2:rows) {
            myht <- c(myht,1/rows)
            if (i >3) rowsd <- rows - 1 - (i/3)
          }
        }
        
        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 = mylegend) # 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, # heights=myht,
                margin = c(input$left, input$right, input$top/rowsd, input$bottom/rowsd)
                )
        
      })
      
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2020-10-31
      • 2021-05-10
      • 1970-01-01
      • 2014-09-12
      • 2019-09-26
      相关资源
      最近更新 更多