【问题标题】:Increase plot size in shiny when using ggplot facets使用 ggplot facets 时增加闪亮的绘图大小
【发布时间】:2018-11-27 14:10:38
【问题描述】:

有没有办法增加shiny 中的绘图窗口大小,具体取决于ggplot 图形中使用的构面数量 - 可能使用垂直滚动。

例如,使用下面的示例,当输入为"A" 时,有三个方面,图看起来不错。选择选项"B" "B"时,绘图的数量增加,但绘图窗口保持相同的大小,从而导致绘图太小。

是否有策略使所有面板高度保持相同,而与输入无关?谢谢。

library(ggplot2)
library(shiny)

mtcars$cyl = sample(letters[1:5], 32, TRUE)

 ui <- fluidPage(
         navbarPage(title="title",
           tabPanel("One", 
                    column(3, 
                           wellPanel( selectInput('name', 'NAME', c("A", "B")))),
                    column(9, plotOutput('plot1')))
   ))


server <- function(input, output) {

    X = reactive({input$name == "A"})

        output$plot1 <- renderPlot({

          if(X()){
             p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( . ~ gear )
          }else{
            p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( cyl  ~ gear )
          }
          return(p1)})
    }



shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    您可以在下面找到一个工作示例:

    library(ggplot2)
    library(shiny)
    library(tidyverse)
    
    
    mtcars$cyl = sample(letters[1:5], 32, TRUE)
    
    gg_facet_nrow<- function(p){
      p %>% ggplot2::ggplot_build()  %>%
      magrittr::extract2('layout')       %>%
      magrittr::extract2('panel_layout') %>%
      magrittr::extract2('ROW')          %>%
      unique()                           %>%
      length()
      }
    
    ui <- fluidPage(
     navbarPage(title="title",
             tabPanel("One", 
                      column(3, 
                             wellPanel( selectInput('name', 'NAME', c("A", "B")))),
                      column(9, plotOutput('plot1')))
    ))
    
    
    server <- function(input, output) {
    
     X <- reactive({input$name == "A"})
    
     p1 <- reactive({
      if(X()){
       p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_grid( . ~ gear )
      }else{
       p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_grid( cyl  ~ gear )
      } 
     return(p1)
     })
    
    he <- reactive(gg_facet_nrow(p1()))
    
    output$plot1 <- renderPlot({p1() }, height = function(){he()*300})
    }
    
    shinyApp(ui,server)
    

    由于以下帖子,这个答案是可能的:

    【讨论】:

      【解决方案2】:

      如果使用facet_wrap而不是facet_grid,则Aurelien callens提供的gg_facet_nrow函数应修改为:

      gg_facet_nrow <- function(p){
          num_panels <- length(unique(ggplot_build(p)$data[[1]]$PANEL)) # get number of panels
          num_rows <- wrap_dims(num_panels)[1] # get number of rows
      }
      

      如果你已经定义了列数,那么函数可以写成如下:

      gg_facet_nrow <- function(p){
          num_panels <- length(unique(ggplot_build(p)$data[[1]]$PANEL)) # get number of panels
          num_cols <- ggplot_build(p)$layout$facet$params$ncol # get number of columns set by user
          num_rows <- wrap_dims(num_panels, ncol=num_cols)[1] # determine number of rows
      }
      

      除了更改为facet_wrap外,Aurelien callens answer中提供的其他代码保持不变。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 2023-02-12
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        • 2018-05-21
        相关资源
        最近更新 更多