How do you create a stacked percentage bar chart in R Shiny app with two reactive values?

Elijah Appelson

I am trying to make a percentage percent bar chart in R with two reactive values. Below is the code that I am using. I keep getting the error "object 'race' not found" and "object 'gender' not found". However, both "race" and "gender" are in the dataframe "filtered_data()". Do you know how I can fix this?

Here is the tab panel:

tabPanel(title = "Comparison Bar Plot",
                                                    h1("Bar Chart"),
                                                     h3("Adjust"),
                                                     selectInput(inputId = "AxisX", label = "X Axis Variable",
                                                                 choices = c("Race", "Gender"),
                                                                 selected = "Race"),
                                                     selectInput(inputId = "AxisY", label = "Y Axis Variable",
                                                                 choices = c("Race", "Gender"),
                                                                 selected = "Gender"),
                                                     plotlyOutput("comparisonbarplot")
                                            )

And here is the output:

    output$comparisonbarplot <- renderPlotly({
  filtered_data() %>% group_by(switch(input$AxisX, Race=race,Gender=gender), switch(input$AxisY, Race=race,Gender=gender)) %>%
    summarise(count=n()) %>% 
    mutate(perc = count/sum(count)) %>%
    ggplot(aes(x = switch(input$AxisX, Race=race,Gender=gender), y = perc*100, fill=switch(input$AxisY, Race=race,Gender=gender))) +
    scale_fill_manual(values =cbPalette) +
    geom_bar( stat="identity") +
    xlab("Race/Ethnicity") +
    ylab("Percent") +
    labs(title = "Race/Ethnicity and Gender of Defendants \n in Dutchess County Courts Starting Fall 2018", fill = "Gender:")+
    theme_economist() +
    theme(plot.title = element_text(hjust = 0.5))
    })
akrun

We can return a string in switch and then use group_by with across

 output$comparisonbarplot <- renderPlotly({
   fstgrp <- switch(input$AxisX, 
                              Race="race",
                              Gender="gender")
   scndgrp <- switch(input$AxisY,
                             Race="race",
                             Gender="gender")
  filtered_data() %>%
         group_by(across(all_of(c(fstgrp, scndgrp)))) %>%
    summarise(count=n()) %>% 
    mutate(perc = count/sum(count)) %>%
    ggplot(aes(x = .data[[fstgrp]], y = perc*100,
        fill=.data[[scndgrp]])) +
    scale_fill_manual(values =cbPalette) +
    geom_bar( stat="identity") +
    xlab("Race/Ethnicity") +
    ylab("Percent") +
    labs(title = "Race/Ethnicity and Gender of Defendants \n in Dutchess County Courts Starting Fall 2018", fill = "Gender:")+
    theme_economist() +
    theme(plot.title = element_text(hjust = 0.5))
    })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create percentage stacked bar chart in plotly?

How do I create a frequency stacked bar chart however have percentage labels on the bars and frequencies on the y axis, in R?

Reactive bar chart in shiny R

How to create a stacked bar chart in r with ggplot

Creating a Stacked Percentage Bar Chart in R with ggplot

plotting a percentage stacked bar chart in R

How to create IF statement with reactive values in R ( Shiny )

How to Create A Stacked Bar Chart?

How do you set the stack order of the series in a stacked bar chart?

How to create stacked bar chart with a time series and aggregated values

How do I add values to the bars in a Stacked Bar chart in Pygal?

shiny: how to add a reactive bar in shiny app

how do you create highcharts bar chart

How do I create a stacked bar chart with summary data?

R Stacked percentage bar plot with percentage of two factor variables with ggplot

How to organize percentage values on top of a stacked bar chart ggplot2

Stacked Bar Chart by Units & Percentage

Percentage stacked bar chart pandas

Overlayed Stacked Percentage Bar Chart

Summarise and create a stacked bar chart in R

Create Stacked "Progress" Bar Chart in R with ggplot

In R shiny, how do you render a plot for a reactive object?

Plotly in R: How to draw stacked bar chart in a time-series data to show percentage composition?

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

R: ggplot stacked bar chart with counts on y axis but percentage as label

Creating a Stacked Percentage Bar Chart in R with ggplot with labels

R bar chart: Some values are not represented in stacked bar chart

How to create a reactive Donut Chart in Shiny

How to create a stacked bar chart for my problem?