'R and Shiny : problem with variable not displayed on the x-axis

I can't understand why my ggplot isn't correctly interpreted with Shiny. It doesn't take account of the values of the x-axis in the "output$plot_features". If I only put he variable name, for example "bathrooms" it works fine, but when I tried to put the variable itself (features), I don't have any value on the X-axis anymore :( I tried to put the values directly in the declaration without the quotes but then it doesn't recognize the variable at all...

Here is my code :

    output$plot_features <- renderPlot({
      
      features  <- switch(input$features, 
                  "Bathrooms" = "bathrooms",
                  "Accommodates" = "accomodates",
                  "Bedrooms" = "bedrooms",
                  "Beds" = "beds")
      
      price_data %>%
      select(bathrooms,accommodates,bedrooms,beds,price) %>%
      ggplot() +
      geom_point(aes(x = features, y = price)) +
      geom_smooth(mapping = aes(x = features, y = price),method = 'lm')
    })

Here is the result : result with problem on the x-axis



Solution 1:[1]

I finally found a solution :

    output$plot_features <- renderPlot({
      
      features  <- switch(input$features, 
                  "Bathrooms" = price_data$bathrooms,
                  "Accommodates" = price_data$accommodates,
                  "Bedrooms" = price_data$bedrooms,
                  "Beds" = price_data$beds)
      
      price_data %>%
      select(bathrooms,accommodates,bedrooms,beds,price) %>%
      ggplot() +
      geom_point(aes(x = features, y = price)) +
      geom_smooth(mapping = aes(x = features, y = price, method = 'lm'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Camille