0
votes

I would like to know how do I solve the error:

Aesthetics must be either length 1 or the same as the data (437): x, y

I am trying to make a histogram using shiny R: I have created two widgets, and if the user chose "IL" as their choice, the x-axis would be all the counties in "IL" (this is what "x" variable is for later on)

output$hist <- renderPlot({
# Store x and y values to produce the chart
x <- chart_two_data$county[chart_two_data$state == input$x_var]
y <- chart_two_data[[input$y_var]]

title <- "Number of Population Race in county by State"

# Create ggplot hist
ggplot(data = chart_two_data) +
  geom_bar(mapping = aes(x = x, y = y),
           stat = "identity") +
  ggtitle("Population Number Of Each Race Based On State")
}) 

This is my chart, organized from midwest dataset: Data set

1
You have to include the rest of your app as well as the data. - Harlan Nelson
Welcome to StackOverflow! I hope you don't mind that I made some edits to improve the formatting, display the data, etc. Good luck! - Ellen Spertus
Your question is not clear. You have to explain what is x_var and y_var. I can understand from the context that x_var is the state the user wants to select but what is y_var??? I'm guessing its any column name - Vedha Viyash
yes, the y_var would be other column names except state and county - Chloe

1 Answers

1
votes

If you wanted input$x_var to give a choice of State and input$y_var to select the column like 'poptotal' or 'area' then this is the solution. You have to use this line to extract the y value y<-chart_two_data[,input$y_var].

Heres the modified code:

output$hist <- renderPlot({
# Store x and y values to produce the chart
x <- chart_two_data$county[chart_two_data$state == input$x_var]
y<-chart_two_data[,input$y_var]

title <- "Number of Population Race in county by State"

# Create ggplot hist
ggplot(chart_two_data) +
     geom_bar(aes(x = x, y = y),stat = "identity") +
labs(x=paste0("County of State ",input$x_var),y=paste0(input$y_val),title="Population Number Of Each Race Based On State")+
theme(panel.background=element_blank())
}) 

Hope this helps!