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

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Immigration bar chart - Australia 2015 - 2017"),
   
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("year", label = "Year", min = 2015, sep="", 
                     max = 2017, value = 2015, 
                     animate = animationOptions(interval = 500, loop = TRUE))
      ), 
      
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("Barchart")
      )
   )
)

library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)
library(shiny)

Immigration <- read.csv("C:/Users/Harsha Alluri/Downloads/nom123.csv" , stringsAsFactors = TRUE)

Immigration$state <- as.factor(Immigration$state)

Immigration$Year <- as.factor(Immigration$Year)

Immigration$Staus <- as.factor(Immigration$Staus)

Immigration$Figures <- as.numeric(Immigration$Figures)

str(Immigration)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
   output$Barchart <- renderPlot({
     p1 <- ggplot(Immigration, aes(x = Immigration$state, y = Immigration$Figures, 
                                   fill = Immigration$Staus))
     p1 + geom_bar(data = filter(Immigration,Staus == "NOM_arrival" 
                                 & Year == input$year), 
                   stat = "identity") +
       geom_bar(data = filter(Immigration,Staus == "NOM_departure" & 
                                Year == input$year),
                stat = "identity")+
                  geom_bar(data = filter(Immigration,Staus == "NOM"
                                         & Year == input$year),
                           aes(y=Figures*(1)),stat = "identity") +
       
       labs(x = "State", y = "Figures")+
       coord_flip()
   })
}
# Run the application 
shinyApp(ui = ui, server = server)

Hi, welcome!

We can't reproduce your issue since we don't have access to your csv file "C:/Users/Harsha Alluri/Downloads/nom123.csv". To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

I suggest you plot a few example plots without shiny first before you try to build the app. Try this. This works for me with some fake data.

	output$Barchart <- renderPlot({
		ggplot() +
			labs(x = "State", y = "Figures")+
			geom_bar(data=filter(Immigration, Year==input$year),
					 mapping=aes(x=state, y=Figures, fill=Staus), stat="identity") +
			coord_flip()
    })
1 Like

Maybe Immigration$Staus is a typo? One guess could be Immigration$Status.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.