Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stacked bar chart #4

Closed
stla opened this issue Oct 30, 2020 · 8 comments
Closed

Implement stacked bar chart #4

stla opened this issue Oct 30, 2020 · 8 comments

Comments

@stla
Copy link
Owner

stla commented Oct 30, 2020

See stacked-clustered-column-chart.

@stla
Copy link
Owner Author

stla commented Oct 30, 2020

library(rAmCharts4)

dat <- data.frame(
  year     = c("2004", "2005", "2006"),
  europe   = c(10, 15, 20),
  asia     = c( 9, 10, 13),
  africa   = c( 5,  6,  8),
  meast    = c( 7,  8, 12),
  namerica = c(12, 15, 19),
  samerica = c(10, 16, 14)
)

stacks <- list(
  c("europe", "namerica"),
  c("asia", "africa", "meast", "samerica")
)

seriesNames <- list(
  europe = "Europe",
  namerica = "North America",
  asia = "Asia",
  africa = "Africa",
  meast = "Middle East",
  samerica = "South America"
)

amStackedBarChart(
  dat,
  category = "year",
  stacks = stacks,
  seriesNames = seriesNames,
  chartTitle = "Stacked bar chart",
  xAxis = "Year",
  yAxis = "A quantity...",
  theme = "kelly"
)

StackedBarChart

@stla
Copy link
Owner Author

stla commented Oct 30, 2020

rAmCharts4_stackedBarChart

@stla
Copy link
Owner Author

stla commented Oct 30, 2020

@ChiragJhawar This is finished. Here is a Shiny example with an 'Update' button:

library(shiny)
library(rAmCharts4)

ui <- fluidPage(
  br(),
  actionButton("update", "Update", class = "btn-primary"),
  br(), br(),
  amChart4Output("barchart", width = "900px", height = "470px")
)

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

  dat <- data.frame(
    year     = c("2004", "2005", "2006"),
    europe   = c(10, 15, 20),
    asia     = c( 9, 10, 13),
    africa   = c( 5,  6,  8),
    meast    = c( 7,  8, 12),
    namerica = c(12, 15, 19),
    samerica = c(10, 16, 14)
  )
  newdat <- data.frame(
    year     = c("2004", "2005", "2006"),
    europe   = c( 7, 12, 16),
    asia     = c( 8, 13, 10),
    africa   = c( 7,  7, 10),
    meast    = c( 8,  6, 14),
    namerica = c(10, 17, 17),
    samerica = c(12, 18, 17)
  )
  stacks <- list(
    c("europe", "namerica"),
    c("asia", "africa", "meast", "samerica")
  )
  seriesNames <- list(
    europe = "Europe",
    namerica = "North America",
    asia = "Asia",
    africa = "Africa",
    meast = "Middle East",
    samerica = "South America"
  )

  output[["barchart"]] <- renderAmChart4({
    amStackedBarChart(
      dat,
      category = "year",
      stacks = stacks,
      seriesNames = seriesNames,
      yLimits = c(0, 60),
      chartTitle = amText(
        "Stacked bar chart",
        fontFamily = "Trebuchet MS",
        fontSize = 30,
        fontWeight = "bold"
      ),
      xAxis = "Year",
      yAxis = "A quantity...",
      theme = "kelly",
      threeD = TRUE
    )
  })

  observeEvent(input[["update"]], {
    updateAmBarChart(session, "barchart", newdat)
  })

}

shinyApp(ui, server)

@ChiragJhawar
Copy link

Depression Bar 3
Hi, thank you so much for doing this. It's amazing. I was asking about these because I wanted to drag a nested/stacked chart. Perhaps it would be explained better with an image. I am trying to Implement a probability chart like in the above image where depression is x and non depression is 1-x. I am also trying to implement a condition inside the chart of probability of tests correctly to diagnose these. I want the user to be able to drag both the white and the grey area.

@stla
Copy link
Owner Author

stla commented Oct 31, 2020

Hmm I'm not sure this is possible. I will think about it.

@stla
Copy link
Owner Author

stla commented Oct 31, 2020

I think this is not possible by dragging (or highly complicated). But you could use some Shiny sliders, as in the following example.

rAmCharts4_survival

library(shiny)
library(rAmCharts4)

probs <- c(control = 30, treatment = 75)

ui <- fluidPage(
  br(),
  sidebarLayout(
    sidebarPanel(
      wellPanel(
        tags$fieldset(
          tags$legend("Survival probability"),
          sliderInput(
            "control",
            "Control group",
            min = 0, max = 100, value = probs[["control"]], step = 1
          ),
          sliderInput(
            "treatment",
            "Treatment group",
            min = 0, max = 100, value = probs[["treatment"]], step = 1
          )
        )
      )
    ),
    mainPanel(
      amChart4Output("barchart", width = "500px", height = "400px")
    )
  )
)

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

  dat <- data.frame(
    group = c("Control", "Treatment"),
    alive = c(probs[["control"]], probs[["treatment"]]),
    dead  = 100 - c(probs[["control"]], probs[["treatment"]])
  )
  stacks <- list(
    c("alive", "dead")
  )
  seriesNames <- list(
    alive = "Alive",
    dead  = "Dead"
  )

  output[["barchart"]] <- renderAmChart4({
    amStackedBarChart(
      dat,
      category = "group",
      stacks = stacks,
      seriesNames = seriesNames,
      yLimits = c(0, 100),
      chartTitle = amText(
        "Survival probabilities",
        fontFamily = "Trebuchet MS",
        fontSize = 30,
        fontWeight = "bold"
      ),
      xAxis = "Group",
      yAxis = "Probability",
      theme = "dataviz"
    )
  })

  observeEvent(list(input[["control"]], input[["treatment"]]), {
    newdat <- data.frame(
      group = c("Control", "Treatment"),
      alive = c(input[["control"]], input[["treatment"]]),
      dead  = 100 - c(input[["control"]], input[["treatment"]])
    )
    updateAmBarChart(session, "barchart", newdat)
  })

}

shinyApp(ui, server)

@ChiragJhawar
Copy link

Hello, sorry for the delayed response. I was off of this for sometime. Thank you so much for this. This seems a lot of hard work and effort and it's amazing. Is there absolutely no way thaat we could make the main (control/treatment) graphs draggable? This is why I was thinking that overlapping the renders would be a nice idea if it were possible.

@ChiragJhawar
Copy link

ChiragJhawar commented Nov 27, 2020

Hi, do you have experience using r2d3? if yes, is it possible to implement https://bl.ocks.org/AlainRo/9264cd08e341f2c92f020c39642c34d1 in that. I have tried to use this, it prints only the background and not the data itself. It's because in D3, we can render graphs on one another which would solve my problem. Thanks.

@stla stla closed this as completed Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants