r - 使用 ggplot facets 时增加 Shiny 的绘图大小

标签 r ggplot2 shiny

有没有办法增加 shiny 中的绘图窗口大小?取决于 ggplot 中使用的方面的数量图 - 也许使用垂直滚动。

例如使用下面的例子,当输入为"A"有三个方面,情节看起来不错。 When the option "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)

最佳答案

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

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)

由于以下帖子,这个答案是可能的:
  • Shiny : variable height of renderplot ( height = function(){he()*300}) )
  • Extract number of rows from faceted ggplot ( gg_facet_nrow() )。
  • 关于r - 使用 ggplot facets 时增加 Shiny 的绘图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914398/

    相关文章:

    R - 具有预定义总数的整数的随机近似正态分布

    r - dplyr 重命名 "object not found"

    r - 与标准一起过滤多列 - R

    r - 沿一个面轴解析标签,沿另一个面轴未解析标签

    r - 计算R中变量连续重复的次数

    减少 ggplot2 中离散轴刻度之间的空间

    r - 如何将 cairo 设置为 R 中 x11() 的默认后端?

    r - 如何在数字输入中添加货币符号

    r - Shiny 的 4 个并排的小文本输入框

    mysql - 如何让 Shiny 的应用程序与云关系数据库(在 MySQL 中)对话?