是否有一种方法可以增加shiny中的绘图窗口大小,取决于ggplot图形中使用的方面的数量--也许是使用垂直滚动。
例如,使用下面的示例,当输入为"A"时,有三个方面,图看起来很好。当选择"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)发布于 2018-06-19 02:54:51
您可以在下面找到一个工作示例:
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)由于下列职位,才有可能作出这一答复:
height = function(){he()*300}))gg_facet_nrow())。发布于 2019-01-09 19:46:11
如果使用facet_wrap而不是facet_grid,则应将Aurelien提供的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答案中提供的其他代码也保持不变。
https://stackoverflow.com/questions/50914398
复制相似问题