How to change the height of a plot in the shiny context

Viewed 681

As shown in the image below, the height of my plot remains constant when multiple counties are selected in the sidebar. This makes the plot too crowded so that the labels become unreadable. I would like to alter the height of my plot as a function of the number of counties that are selected in the sidebar. How can I do this?

Below, please see part of my codes that I believe need to be modified.

enter image description here

UI element

mainPanel(plotOutput("workforce_plot"), 

server element

workforce_plot <- reactive({
     ggplot(workforce_data(), aes(x =Age_Group, y = Percentage, fill=County.y)) + 
     geom_bar(stat = "identity",position = position_dodge()) +                                                         
     geom_text(
     aes(label = Percentage2),
     vjust = 0.5,
     colour = "black", 
     position = position_dodge(width=0.9),
     fontface = "bold",
     size=3.2,
     hjust = 0)+
     ylim(0,110)+        
     coord_flip()+

     labs(
          x = "Age Groups",
          y = "Percentage",
          face = "bold")
          })

   output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          })
1 Answers

You can add a height parameter to your renderPlot function, and pass a function to it that adjusts the height based on some relevant reactive.

One example:

output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          }, height = function() {200 + (length(workforce_data()[["County.y]]) *.2)})

(It could also be something like length(input$country) instead of a reactive expr.)

Then in ui.R define height = 'auto' for that panel.

Related