Title: R and Shiny : problem with variable not displayed on the x-axis
Tags: r;variables;ggplot2;shiny
Question: 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 &quot;output$plot_features&quot;.
If I only put he  variable name, for example &quot;bathrooms&quot; 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 michaelclark@example.com...
Here is my code :
output$plot_features <- renderPlot({

features  <- switch(input$features,
&quot;Bathrooms&quot; = &quot;bathrooms&quot;,
&quot;Accommodates&quot; = &quot;accomodates&quot;,
&quot;Bedrooms&quot; = &quot;bedrooms&quot;,
&quot;Beds&quot; = &quot;beds&quot;)

price_data %&gt;%
select(bathrooms,accommodates,bedrooms,beds,price) %&gt;%
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
Comment: Try with `aes_string(x = features, y = "price")` as you want to plot the column whose name is stored in the variable `features`.

Comment: `features` is a string, not a column name, so tidyverse’s NSE fails. You need to quote it or use `aes_string`. Welcome to SO, but please be aware of the importance of *minimal* in “minimal reproducible example”.

Comment: Thanks for your reply, and sorry for the length of the code.
I already tried aes_string with doublequotes, but the problem is still similar :
aes_string(x = "features", y = "price") doesn't seem to work better :(


Here is another answer: I finally found a solution :
output$plot_features <- renderPlot({

features  <- switch(input$features,
&quot;Bathrooms&quot; = price_data$bathrooms,
&quot;Accommodates&quot; = price_data$accommodates,
&quot;Bedrooms&quot; = price_data$bedrooms,
&quot;Beds&quot; = price_data$beds)

price_data %&gt;%
select(bathrooms,accommodates,bedrooms,beds,price) %&gt;%
ggplot() +
geom_point(aes(x = features, y = price)) +
geom_smooth(mapping = aes(x = features, y = price, method = 'lm'))
Comment for this answer: While this works you have to be aware that it is in general not recommend to map dataframe columns directly on aesthetics. And quite often it is the reason for issues and errors. The recommended way would be to use `aes_string`(see my comment on that) or to use the `.data` pronoun, i.e. aes(x = .data[[features]], ...`.
Comment for this answer: Hello, thanks for your comment. I tried your way and indeed it works fine :) (I'm very new to R and I do basic mistakes).
Thanks equally for the explication about potential problems on my solution with aestetics.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:04/03/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><variables><ggplot2><shiny>', 'Title': 'R and Shiny : problem with variable not displayed on the x-axis', 'ViewCount': 69, 'id': 71728179, 'user_AboutMe': None, 'user_AccountId': 24812248.0, 'user_DisplayName': 'Camille', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 18693555.0}}