Using input$ on ggplot

ggplot2
shiny
rstudio

#1

I am trying to add the output from a drop down list into a field in ggplot

The data fields I want to use has these fields:
dbrow

The code I use to output this in a chart is:

ggplot(survey_data_sub, aes(fill=`Education level`, y=`Education level`, 
x=`Developer group`)) +
geom_bar( stat="identity")

This works great for showing the Education level results.

I want to make the fill and y dynamic, from a drop down list. So I used the below shiny code:

sidebarLayout(
           sidebarPanel(
             selectInput("newplot_select", "Choose Breakdown for chart:", 
                         list("UK location", "Education level", "Current 
salary", "Generation"))
               ),

               mainPanel(
                 textOutput("newPlot_selected_filter"),
                 plotOutput("newPlot")
               )
             )


 output$newPlot <- renderPlot({

    ggplot(survey_data_sub, aes(fill=input$newplot_select, 
    y=input$newplot_select, x=`Developer group`)) + 
    geom_bar( stat="identity")

 })

the input$newplot_select doesent pull in the content from the dropdown list. If I just output the input$newplot_select in a text output it does print the input value. How can I do that in the ggplot code I have used?

Thank you for your help


#2

There may be a better way to do this and it may not be needed once tidyeval is implemented in ggplot2 but you could do this:

output$newPlot <- renderPlot({
    select_quo <- quo(input$newplot_select)

    survey_data_sub %>%
      mutate(user_input = !!select_quo) %>%
      ggplot(aes(fill=user_input,  y=user_input, x=`Developer group`)) + 
      geom_bar( stat="identity")

 })

This just creates a new column in the dataset that is a copy of the selected column by the user and then passes that column to the ggplot call. As a note, you will need to have dplyr loaded if you do not already.


#3

thank you for this, when I run it an error gets thrown at:

survery_data_sub %>%

im guessing this is my data. How do I load my data into that. Currently i set my data at the top of my file to load in like:

load("survey_data_sub.Rda")

#4

There was a typo in my answer. I fixed it so the data set should have the correct name (i.e. the same as the one in your question). It looks like I inadvertently added a 'r' at the end of the word survey.


#5


#6

as you can see on the 2 images. the all pink one is the one using your code and the other one is just manually adding in the words Education level. It looks like it is only plotting 1 lot of data instead of multiple


#7

This is how the data is displayed in the table if it helps...