Search code examples
rshinydiagram

How to change range on the geom_bar


I have a Shiny application which should show dependency between numbers (column count) and race of workers in the Silicon Valley. I want that when I choose on the left side gender, job category and company on the diagram will be shown count of workers with different races. Now my diagram has range for the count only from 0 to 1 and shows not correct diagram.

Here is my code:

library(shiny)
library(ggplot2)
library(dplyr)

bcl <- read.csv(file = "E:/country/data/reveal.csv", colClasses =   c("character", "integer", "factor", "factor", "factor", "integer"), na.strings = c("na", "NA")) %>% na.omit()

ui <- fluidPage(
titlePanel("Silicon Valley Diversity Data"),
sidebarLayout(
  sidebarPanel(
  img(src = "silicon.png", height = 150, width = 250),br(),
  em("Choose company, job category and gender"),br(),
     radioButtons("genderInput", "gender", 
                         choices = list("male" = "male", 
                                         "female" = "female"),
                          selected = "male"),
     radioButtons("jobInput","Job category",
                  choices = c(
                    "First/Mid officials & Mgrs",
                    "Professionals",
                    "Administrative support",
                    "Sales workers"
                  ),
                  selected = "Technicians"
                ),
                selectInput("companyInput", "company",
                            choices = c("Adobe", "Cisco",  "Facebook", "Google", "HP", "Intel", "Twitter"))
              ),
              mainPanel(plotOutput("coolplot"),
                        br(), br(),
                        tableOutput("results"))
            ))

server <- function(input, output) {
output$coolplot <- renderPlot({
filtered <-
  bcl %>%
  filter(
    gender == input$genderInput,
    job_category == input$jobInput,
    company == input$companyInput
  )
ggplot(filtered, aes(race)) +
  geom_bar(fill = "#9f3e74")
})
}

shinyApp(ui = ui, server = server)

Here is my app:

enter image description here


Solution

  • Try this:

    library(shiny)
    library(ggplot2)
    library(dplyr)
    
    # Load the data
    df <- read.csv(file = "reveal.csv",
        colClasses = c("character", "integer", "factor", "factor", "factor", "integer"),
        na.strings = c("na", "NA"))
    
    # Clean the data
    bcl <- df %>% 
      na.omit() %>% 
      select(-year) %>% 
      filter(count != 0) %>%
      filter(job_category != "Totals", 
             job_category != "Previous_totals",
             race != "Overall_totals", 
             race != "Two_or_more_races")
    
    ui <- fluidPage(
      titlePanel("Silicon Valley Diversity Data"),
      sidebarLayout(
        sidebarPanel(
          img(
            src = "silicon.png",
            height = 150,
            width = 250
          ),
          br(),
          em("Choose company, job category and gender"),
          br(),
          radioButtons(
            "genderInput",
            "gender",
            choices = list("male" = "male",
                           "female" = "female"),
            selected = "male"
          ),
          selectInput(
            "jobInput",
            "Job category",
            choices = unique(bcl$job_category)  # get choices programmatically
          ),
          selectInput(
            "companyInput",
            "company",
            choices = unique(bcl$company)  # get choices programmatically
          )
        ),
        mainPanel(plotOutput("coolplot"),
                  br(), br(),
                  tableOutput("results"))
      )
    )
    
    server <- function(input, output) {
    
      output$coolplot <- renderPlot({
    
        filtered <- filter(
          bcl,
          company == input$companyInput,
          gender == input$genderInput,
          job_category == input$jobInput)
    
        df <- filtered[rep(row.names(filtered), filtered$count), 1:4]  #  prepare data for plotting
    
        ggplot(df, aes(race)) + geom_bar() + coord_flip()
    
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    In sum, the solution involved doing some more cleaning of the data, and some final manipulation once the filtering was performed in server. I also took the liberty of doing some changes in the ui. I hope you like it.