0

1. How to use selectinput and grouped bar chart to make selectinput=States,x-axis = group age, y-axis = population, type = male and female?

2. Do not know how to selectinput link data file.csv made of stack bar chart?

data file:

type    States          age    population
male    "TaipeiCity "   0~19    12345
male    "TaipeiCity "   20~39   54321
male    "TaipeiCity "   40~59   6789
male    "TaipeiCity "   60~79   9876
male    "TaipeiCity "   80~100  5566
female  "TaipeiCity "   0~19    123456
female  "TaipeiCity "   20~39   654321
female  "TaipeiCity "   40~59   987654
female  "TaipeiCity "   60~79   556655
female  "TaipeiCity "   80~100  111111
male    NewTaipeiCity   0~19    123
male    NewTaipeiCity   20~39   456
male    NewTaipeiCity   40~59   789
male    NewTaipeiCity   60~79   987
male    NewTaipeiCity   80~100  654
female  NewTaipeiCity   0~19    1234
female  NewTaipeiCity   20~39   5678
female  NewTaipeiCity   40~59   9876
female  NewTaipeiCity   60~79   5432
female  NewTaipeiCity   80~100  1995

code:

        library(shiny)
        library(ggvis)
        library(dplyr)
        library(ggplot2)
        library(RColorBrewer)
        test <- read.csv("C:/Users/user/Documents/barchart/1995.csv")

        ui <- shinyUI(fluidPage(

          sidebarLayout(
            sidebarPanel(

              selectInput("bg_states", label = h3("region:"),c("TaipeiCity","NewTaipeiCity"))

            ),
            mainPanel(
              h3("Man and woman age stack bar graph"),
              ggvisOutput("mytest")
            )
          )

        ))

        server <- shinyServer(function(input,output){

          mytest <- reactive({
           ym <- unique(as.character(input$bg_states))
           test %>%

           group_by(age,type) %>%
           summarise(population = n()) %>%
           ggvis(~age,~population) %>%
           layer_bars(fill = ~type,width = 0.5)
          })
          mytest %>% bind_shiny("mytest")

        })
        shinyApp(ui,server)

screen:

enter image description here

ycluo
  • 3
  • 5

1 Answers1

1

I think this will do what you want it to do: A data summary is not necessary. I left your lines in the code, but made them comments.

library(shiny)
library(ggvis)
library(dplyr)
# library(ggplot2)
# library(RColorBrewer)
test <- read.table("testdata.txt", header = T)

ui <- shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(

      selectInput("bg_states", label =         h3("region:"),c("TaipeiCity","NewTaipeiCity"))

    ),
    mainPanel(
      h3("Man and woman age stack bar graph"),
      ggvisOutput("mytest")
    )
  )

))

server <- shinyServer(function(input,output){

  mytest <- reactive({
    # ym <- unique(as.character(input$bg_states))

        test %>%
          filter(States == input$bg_states) %>%
      # group_by(age,type) %>%
      # summarise(population = n()) %>%
      ggvis(~age,~population) %>%
      layer_bars(fill = ~type, width = 0.5)
  })
  mytest %>% bind_shiny("mytest")

})
shinyApp(ui,server)

Result:

result graph

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Stolte
  • 26
  • 4
  • Sorry if I want to do a side-by-side bar chart how to do? – ycluo Nov 07 '17 at 16:27
  • have a look here: https://stackoverflow.com/questions/28553760/ggvis-side-by-side-barchart-grouped-by-second-variable – Stolte Nov 08 '17 at 10:22
  • Why system error in layer bars (., Stack = FALSE): y variable (weights) must be numeric.I do not know what to do. – ycluo Nov 09 '17 at 12:51
  • and also Error : Length of logical index vector must be 1 or 10 (the number of rows), not 0 – ycluo Nov 09 '17 at 13:54
  • This answer works perfectly well on R 3.6.1 but doesn't work on R 4.0.0 ! Does someone have an idea why ? – wanderzen Nov 13 '20 at 13:08