Search code examples
rggplot2shinygeom-bar

How to change the size of bar in bar chart in shiny app?


In my shiny app, I want to show the impact of user on a specific site (site 1 till site 10) compared to the impact on all sites.

enter image description here

The app is working fine. However I want to change the size of the bar. I couldn't change the bar size using geom_bar(stat = "identity", size = 0.35)

I want to change the bar size especially when I'm selecting allsites because the size becomes so wide

enter image description here

Any suggestions will be highly appreciated

Code

library(shiny)
library(tidyverse)

load("df.rdata")

ui <- fluidPage(
fluidRow(
  column(2,
         radioButtons(inputId = "site",
             label = "Select site",
             choiceNames = c("allsites", "site1", "site2", "site3", "site4", "site5", "site6", "site7", "site8", "site9", "site10"),
             choiceValues = c("allsites", "site1", "site2", "site3", "site4", "site5", "site6", "site7", "site8", "site9", "site10"),
             selected = "allsites")
),
column(10,
  fluidRow( 
  tabsetPanel(type = "tabs",
              tabPanel("Bar chart", 
                       tags$br(),
                       tags$br(),
                       plotOutput("bar_chart", width = "75%")
              )
  )
  )
)
)
)


server <- function(input, output, session){

site <- reactive({
  input$site
})


output$bar_chart <- renderPlot({

  df1 <- df %>%
    dplyr::filter(site %in% c("allsites", site()))


  ggplot(df1, aes(site, median, fill = site))+
    geom_bar(stat = "identity", size = 0.35)+
    geom_errorbar(aes(ymin = lower, ymax = upper),
                  width = .09, size=0.2)+
    facet_wrap(~site, ncol = 2, scales ="free")

  })

}

shinyApp(ui, server)

Data

> dput(df)
structure(list(site = structure(c(1L, 2L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 3L), .Label = c("allsites", "site1", "site10", 
"site2", "site3", "site4", "site5", "site6", "site7", "site8", 
"site9"), class = "factor"), lower = c(40.97, 1.92, 7.5, 2.66, 
1.18, 0.72, 6.92, 6.87, 3.41, 2.17, 4.2), median = c(43.18, 2.56, 
8.87, 3.17, 1.84, 1.04, 8.14, 8.1, 4.96, 3.03, 5.87), upper = c(45.54, 
3.64, 10.59, 3.75, 2.63, 1.65, 9.49, 9.45, 6.18, 4.04, 8.15)), class = "data.frame", row.names = c(NA, 
-11L))

Solution

  • We can use width instead of size in the geom_bar function.

    enter image description here

    Code

    library(shiny)
    library(tidyverse)
    
    df <- structure(list(site = structure(c(1L, 2L, 4L, 5L, 6L, 7L, 8L, 
                                            9L, 10L, 11L, 3L), .Label = c("allsites", "site1", "site10", 
                                                                          "site2", "site3", "site4", "site5", "site6", "site7", "site8", 
                                                                          "site9"), class = "factor"), lower = c(40.97, 1.92, 7.5, 2.66, 
                                                                                                                 1.18, 0.72, 6.92, 6.87, 3.41, 2.17, 4.2), median = c(43.18, 2.56, 
                                                                                                                                                                      8.87, 3.17, 1.84, 1.04, 8.14, 8.1, 4.96, 3.03, 5.87), upper = c(45.54, 
                                                                                                                                                                                                                                      3.64, 10.59, 3.75, 2.63, 1.65, 9.49, 9.45, 6.18, 4.04, 8.15)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                         -11L))
    
    
    
    
    ui <- fluidPage(
      fluidRow(
        column(2,
               radioButtons(inputId = "site",
                            label = "Select site",
                            choiceNames = c("allsites", "site1", "site2", "site3", "site4", "site5", "site6", "site7", "site8", "site9", "site10"),
                            choiceValues = c("allsites", "site1", "site2", "site3", "site4", "site5", "site6", "site7", "site8", "site9", "site10"),
                            selected = "allsites")
        ),
        column(10,
               fluidRow( 
                 tabsetPanel(type = "tabs",
                             tabPanel("Bar chart", 
                                      tags$br(),
                                      tags$br(),
                                      plotOutput("bar_chart", width = "75%")
                             )
                 )
               )
        )
      )
    )
    
    
    server <- function(input, output, session){
    
      site <- reactive({
        input$site
      })
    
    
      output$bar_chart <- renderPlot({
    
        df1 <- df %>%
          dplyr::filter(site %in% c("allsites", site()))
    
    
        ggplot(df1, aes(site, median, fill = site))+
          geom_bar(stat = "identity", width = 0.35)+
          geom_errorbar(aes(ymin = lower, ymax = upper),
                        width = .09, size=0.2)+
          facet_wrap(~site, ncol = 2, scales ="free")
    
      })
    
    }
    
    shinyApp(ui, server)