Home > Net >  Rshiny : y axis based on selectinput for barplot but with a fixed x axis
Rshiny : y axis based on selectinput for barplot but with a fixed x axis

Time:12-25

Here is my data :

years   bank    hospital    school  police  housing

2016    125       436        248    7.612   2.535
2017    167       497        281    8.215   3.145
2018    6064      2792      7564    38015   329
2019    2.972     3.070     14.771  40.180  371
2020    193       2.417     10.472  47.894  366



dput(fund)
structure(list(sector = c(2016, 2017, 2018, 2019, 2020), bank = c(125.2243, 
167.4207, 6064.2903, 2971.6812, 192.7661), hospital = c(435.6579, 
496.557, 2791.8501, 3070.4745, 2416.9531), school = c(248.476, 
281.4527, 7564.4581, 14770.9388, 10471.76), police = c(7612.456, 
8215.4141, 38014.6638, 40179.971, 47894.2436), housing = c(2534.8019, 
3145.1998, 328.9033, 370.7749, 365.9282)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

and this is the code I made (I'm stuck at the server part):

library(shiny)

ui <- fluidPage(titlePanel("Fund"),
                selectInput(
                  inputId = "varbar1",
                  "choose....",
                  choices = c('bank',
                              'hospital',
                              'school',
                              'police',
                              'housing'
                  ),selectize=FALSE),
                plotOutput("bar1"))
server <- function(input, output) {
  
}
shinyApp(ui = ui, server = server)

I want my output to be like this :

enter image description here

Can someone tell me how can this work especially the coding in the server section please

CodePudding user response:

The server part can be

server <- function(input, output) {
  
  output$bar1 <- renderPlot({
    
    barplot(fund[[input$varbar1]], names.arg = fund$sector, col = "red")
  })
  
}
shinyApp(ui = ui, server = server)

-output

enter image description here

CodePudding user response:

i found another solution for this problem, but this time i use ggplot2 and reshape2 here is the code :

library(shiny)
library(ggplot2)
library(reshape2)

ui <- fluidPage(titlePanel("Fund"),
                selectInput(
                  inputId = "varbar1",
                  "choose....",
                  choices = c('bank',
                              'hospital',
                              'school',
                              'police',
                              'housing'
                  ),selectize=FALSE),
                plotOutput("bar1"))

server <- function(input, output) {
  output$bar1<-renderPlot({

    #reshape data
    dfm <- melt(fund[,c("sector",input$varbar1)],id.vars = 1)

    #barplot
    ggplot(dfm,aes(x = factor(sector),y = value))   
      geom_bar(aes(fill = variable),stat = "identity",position = "dodge")   
      theme_minimal()  
      theme(panel.grid.major.y = element_blank(),
            panel.grid.minor.y = element_blank(),
            panel.grid.minor.x = element_blank())
  })
}
shinyApp(ui = ui, server = server)

and here is the result : enter image description here

  • Related