【问题标题】:Change height of plot depending of number of groups for different input selections根据不同输入选择的组数更改图的高度
【发布时间】:2023-02-02 17:04:33
【问题描述】:

我想根据特定选择 (V1) 的数据集中的变量数 (Column Cancer) 更改绘图的高度。我的零工输出是p,我尝试使用out <- layer_data(p, 1)获取组数,然后使用公式length(table(out$group))*50计算地块的高度,但是,所有地块都是相同的,无论我有多少类别在我的 V1 变量中。

server <- function(input, output, session) {
  
  data_selected <- reactive({
    filter(files.Vir.DNA.df.test, V1 %in% input$Taxa)
  })
  
  output$myplot1 <- renderPlot({
    #data_selected() %>%
    p <- ggplot(data_selected(),aes(position,rowSums, fill = Cancer)) + 
      geom_bar(stat="identity") +
      facet_grid(Cancer~. , scales = "free_x", space = "free_x", switch = "x") +
      theme(strip.text.y = element_text(angle = 0),
            strip.text.x = element_text(angle = 90),
            strip.background = element_rect(colour = "transparent", fill = "transparent"),
            plot.background = element_rect(colour = "white", fill = "white"),
            panel.background = element_rect(colour = "white", fill = "white"),
            
            axis.text.x = element_blank(),
            axis.ticks.x = element_blank()) + 
      labs(y="", x="", title="") +
      scale_fill_manual(values=mycolors) + 
      theme(legend.position = "none") +
      scale_y_log10(breaks=c(1,100,10000)) 
      print(p)
      out <- layer_data(p, 1)
    
  },res = 100,width = 600, height = length(table(out$group))*50)
}

【问题讨论】:

    标签: shiny


    【解决方案1】:

    你的例子不可重现 - 所以我做了一个新的。请检查以下内容:

    library(shiny)
    library(ggplot2)
    library(datasets)
    
    ui <- fluidPage(
      selectizeInput("species", "Species", choices = unique(iris$Species), selected = unique(iris$Species), multiple = TRUE, options = list('plugins' = list('remove_button'))),
      fluidRow(plotOutput("myplot"))
    )
    
    server <- function(input, output, session) {
      filtered_data <- reactive({
        iris[iris$Species %in% input$species,]
      })
      
      output$myplot <- renderPlot({
        scatter <- ggplot(data = filtered_data(), aes(x = Sepal.Length, y = Sepal.Width)) 
        scatter + geom_point(aes(color=Species, shape=Species)) +
          xlab("Sepal Length") +  ylab("Sepal Width") +
          ggtitle("Sepal Length-Width")
      }, height = reactive({length(input$species)*200+200}))
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2012-11-19
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      相关资源
      最近更新 更多