使用ggplot构面时增加闪亮的绘图大小 [英] Increase plot size in shiny when using ggplot facets
问题描述
是否有一种方法可以增加shiny
中的绘图窗口大小,具体取决于ggplot
图形中使用的构面的数量-也许使用垂直滚动.
Is there a way to increase the plot window size in shiny
dependent on the number of facets that are used in a ggplot
figure - perhaps using a vertical scroll.
例如,使用下面的示例,当输入为"A"
时,将具有三个方面,并且图看起来不错.选择选项"B"
时,图的数量增加,但图窗口保持相同大小,导致图太小.
For example, using the example below, when the input is "A"
there are three facets and the plots look good. When the option "B"
is selected the number of plots increases but the plot window stays the same size resulting in plots that are too small.
是否有一种策略可以使所有面板的高度保持不变,而与输入无关?谢谢.
Is there a strategy to keep all the panel heights the same, independent of input? Thanks.
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)
推荐答案
您可以在下面找到一个有效的示例:
You can find a working example below :
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)
由于以下帖子,此答案得以实现:
This answer was possible thanks to the following posts :
- 发光:可变高度的renderplot (
height = function(){he()*300})
) - 从多面ggplot中提取行数(
gg_facet_nrow()
).
- Shiny : variable height of renderplot (
height = function(){he()*300})
) - Extract number of rows from faceted ggplot (
gg_facet_nrow()
).
这篇关于使用ggplot构面时增加闪亮的绘图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!