简体   繁体   中英

MultiBarChart with HighCharter in shiny app

Is there a way to plot a graph like this one with highcharter package in Shiny app?

https://www.highcharts.com/demo/column-stacked-and-grouped

Here's my dataframe:

names <- c("Sr. Blue","Sr. Green","Sr. Brown","Sr. Yellow","Sr. Purple","Sr. Fish","Sr. Greek","Sr. Red","Sr. Dark")
names <- names[rep(seq_len(length(names)), each=5)]
teachers <- data.frame(name = names, age = rep(c("0-4","4-9","9-15","15-20","+20"),9),students = sample(1:50,45))

The objective is to show students~names grouped by age.

Here is an example of how to plot stacked column graph in Highcharts (Highcharter) and Shiny:

library(shiny)
library(highcharter)
ui <- fluidPage(
  h1("Highcharts Shiny stacked column demo"),
  fluidRow(
    column(width = 12, highchartOutput("hcontainer",height = "500px"))
  )
)
server = function(input, output) {
  output$hcontainer <- renderHighchart({
    hc <- highchart() %>%
      hc_chart(type = 'column') %>%
      hc_xAxis(categories = c('Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas')) %>%
      hc_plotOptions(series = list(stacking = 'normal')) %>%
      hc_add_series(
        name = 'John',
        data = list(5, 3, 4, 7, 2),
        stack = 'male'
      ) %>%
      hc_add_series(
        name = 'Joe',
        data = list(3, 4, 4, 2, 5),
        stack = 'male'
      ) %>%
      hc_add_series(
        name = 'Jane',
        data = list(2, 5, 6, 2, 1),
        stack = 'female'
      ) %>%
      hc_add_series(
        name = 'Janet',
        data = list(3, 0, 4, 4, 3),
        stack = 'female'
      )
    hc
  })
}
shinyApp(ui = ui, server = server)

I am not R developer so I don't know how exactly does your data look like, but if you give me more details about x categories, series names and y values, I will try to help with finishing your chart and edit my answer.

Here is a pure JS code of the above chart: https://jsfiddle.net/BlackLabel/wgthu2L5

And some API References might be useful: https://api.highcharts.com/highcharts/plotOptions.series.stacking https://api.highcharts.com/highcharts/series.column.stack https://api.highcharts.com/highcharts/xAxis.categories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM