【问题标题】:Autosize height of ggplotly in shiny在闪亮时自动调整ggplotly的高度
【发布时间】:2020-06-04 03:57:32
【问题描述】:

我有一个很高的plotlyOutput(因为它在y轴上有很多类别)。如果它只用ggplotly(g) 渲染,我得到这个图,高度固定:

所以我指定它是height,像这样:ggplotly(g, height = 1500),我得到一个合适的大小(以下是情节的一小部分,因为它的大小):

我的问题是,当我通过应用程序的输入控件过滤数据时,它会保持高度(因为它现在已修复),即使 y 轴上的类别很少。像这样(以下是情节的一小部分,因为它的大小):

所以,¿我怎样才能获得我的 plotlyOutput 的动态高度,例如,当 y 轴上有 3 个类别时,它将 height 调整为更短,但最大为 @987654333 @随着类别数量的增加?

【问题讨论】:

    标签: shiny plotly


    【解决方案1】:

    除了这个,我没有看到任何解决方案,它根据图中的类别数计算高度:

    library(shiny)
    library(plotly)
    
    dat <- data.frame(
      Letter = c(LETTERS,letters),
      height = rpois(52, 10)
    )
    
    ui <- fluidPage(
      checkboxInput("filter", "Filter"),
      plotlyOutput("ggly", height = "100%")
    )
    
    server <- function(input, output, session){
    
      output[["ggly"]] <- renderPlotly({
        ggdat <- if(input[["filter"]]) dat[dat$Letter %in% c("A","B"),] else dat
        height <- nlevels(droplevels(ggdat$Letter)) * 30
        gg <- ggplot(ggdat) + geom_col(aes(x = Letter, y = height)) + coord_flip()
        ggplotly(gg, height = min(1500, max(300, height)))
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢...这是一个很好的解决方法!如果我能给你 5 个赞!
    猜你喜欢
    • 2021-02-13
    • 2017-12-27
    • 1970-01-01
    • 2014-10-11
    • 2018-08-14
    • 2010-10-17
    • 2019-03-15
    • 2021-05-31
    相关资源
    最近更新 更多