【问题标题】:Dynamic height of shiny ggplotly plot闪亮的ggplotly图的动态高度
【发布时间】:2021-02-13 23:27:14
【问题描述】:

我有一个基于geom_col()ggplotly 输出,其中的条数变化很大。我想对其进行配置,使条形之间的间距(以及条形宽度)无论有多少条形都保持不变,这意味着改变绘图高度。以下基于this solution,但对我没有影响。任何建议表示赞赏。

require(tidyverse)
require(shiny)
require(plotly)

ui = fluidPage(
  sidebarPanel(width = 3, 
      sliderInput('count', 'count', min = 3, max = 100, value = 100, step = 25)
  ),
  mainPanel(width = 9, 
      div(plotlyOutput("plot", height = '200vh'), 
          style='height:90vh !important; overflow:auto !important; background-color:yellow;')
  )
)

server <- function(input, output, session) {
  
  output$plot = renderPlotly({
    d = data.frame(x = head(sentences, input$count), y = rlnorm(input$count, meanlog = 5))
    p = d %>% ggplot(aes(fct_reorder(x, y), y)) +
      geom_col(width = 0.1, col='grey90') + geom_point(size = 2) + 
      coord_flip() +
      theme_minimal(base_size = 12) + theme(panel.grid.major.y = element_blank())
    pltly = ggplotly(p) %>% layout(xaxis = list(side ="top" ))
    pltly$height = nrow(d) * 15
    pltly
  })
  
}

shinyApp(ui = ui, server = server,
         options = list(launch.browser = FALSE))

【问题讨论】:

    标签: r shiny r-plotly ggplotly


    【解决方案1】:

    您可以在ggplotly()plot_ly() 中指定宽度/高度:

    library(tidyverse)
    library(shiny)
    library(plotly)
    
    ui = fluidPage(
      sidebarPanel(width = 3, 
                   sliderInput('count', 'count', min = 3, max = 100, value = 100, step = 25)
      ),
      mainPanel(width = 9, 
                plotlyOutput("plot"),
      )
    )
    
    server <- function(input, output, session) {
      output$plot = renderPlotly({
        d = data.frame(x = head(sentences, input$count), y = rlnorm(input$count, meanlog = 5))
        p = d %>% ggplot(aes(fct_reorder(x, y), y)) +
          geom_col(width = 0.1, col='grey90') + geom_point(size = 2) + 
          coord_flip() +
          theme_minimal(base_size = 12) + theme(panel.grid.major.y = element_blank())
        pltly = ggplotly(p, height = nrow(d) * 15) %>% layout(xaxis = list(side ="top" ))
        pltly
      })
    }
    
    shinyApp(ui = ui, server = server, options = list(launch.browser = TRUE))
    

    但是,您可能希望指定更大的最小高度,使用第一个选项,绘图会变得很窄。

    【讨论】:

    • 我已经尝试过了,但猜想我的 ui 代码正在击败它。谢谢:)
    • 你确定吗?对我来说,它也适用于您的原始 UI 代码(如果在 ggplotly 中调用了 height)。
    • 我认为我的身高计算有问题。
    猜你喜欢
    • 2020-06-04
    • 2018-08-14
    • 1970-01-01
    • 2022-10-04
    • 2015-01-03
    • 2018-12-22
    • 2017-11-03
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多