Stacked bar plot with shiny in r

I am going to sketch a stacked barplot with the shiny app in R, I need some help for the plot section. X-axis: would be Timepoint Group is DM and FM. Timepoint: 7 timepoints. each column would include M0, M1, M2, M3, M4 (cumulative up to 1)

I used ggplot for that!

Thanks for your help!

6 rows of Data:

> head(metab)
Replicate Group  Timepoint        M0       M1     M2 M3 M4   M_Total
1         1  DM         0 462276966 18832499 123172  0  0 481232637
2         2  DM         0 389073706 16540080 104004  0  0 405717790
3         3  DM         0 440497937 18778082  74654  0  0 459350673
4         4  DM         0 339266538 14427218  89319  0  0 353783075
5         5  DM         0 476276612 20352483  51748  0  0 496680843
6         6  DM         0 392164060 16587310 111081  0  0 408862451

code:

library(shiny)

metab <- read.csv("metab.csv")
metab[,c("M0","M1","M2","M3","M4")] <- sapply(metab[,c("M0","M1","M2","M3","M4")],as.numeric)
metab$M_Total <- rowSums(metab[ , c("M0","M1","M2","M3","M4")], na.rm=TRUE)

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      p(style = "font-family:Impact",
        a("metab Expectation")),
      
      selectInput("var",label="Choose an method",choice=c("M0",
                                                                "M1",
                                                                "M2",
                                                                "M3",
                                                                "M4",
                                                                "M_Total"),selectize=FALSE),
      
      selectInput(inputId = "Group", label = strong("Group"),
                  choices = unique(metab$Group),selected = "DM"),
      
      selectInput(inputId = "Timepoint", label = strong("Timepoint"),
                  choices = unique(metab$Timepoint),colnames(metab),
                  selected = 30),
    ),
    column(7, offset = 4,
           h4("Median of selected method:"),
           verbatimTextOutput("median"),
           h4("Median of method proportion:"),
           verbatimTextOutput("mediantotal"),
           plotOutput("box")
    )
  )
)



server <- function(input,output){
  
  output$box <- renderPlot({
    x <- metab[metab$Group == input$Group & metab$Timepoint == input$Timepoint,
                   which(colnames(metab)==input$var)] 
    
    ggplot(data=metab, aes_string(x=input$Timepoint,y=x, fill = "Group"))  + geom_bar(stat="identity") + 
      labs(title="Value") +
      theme_classic() + 
      theme(plot.title = element_text(hjust = 0.5))  })
  
  output$mediantotal <- renderPrint({ 
    median(metab[metab$Group == input$Group & metab$Timepoint == input$Timepoint,
                     which(colnames(metab)==input$var)]) / 
      median(metab[metab$Group == input$Group & metab$Timepoint == input$Timepoint,
                       colnames(metab)== "M_Total"])
  })
  output$median <- renderPrint({ 
    median(metab[metab$Group == input$Group & metab$Timepoint == input$Timepoint,
                     which(colnames(metab)==input$var)])
  })
}

shinyApp(ui = ui, server = server)

Can you provide the output of dput(head(metab))?

dput(head(metab))
structure(list(Replicate = 1:6, Group = c("DM", "DM", "DM", 
                                          "DM", "DM", "DM"), Timepoint = c(0L, 0L, 0L, 0L, 0L, 0L
                                          ), M0 = c(462276966, 389073706, 440497937, 339266538, 476276612, 
                                                    392164060), M1 = c(18832499, 16540080, 18778082, 14427218, 20352483, 
                                                                       16587310), M2 = c(123172, 104004, 74654, 89319, 51748, 111081
                                                                       ), M3 = c(0, 0, 0, 0, 0, 0), M4 = c(0, 0, 0, 0, 0, 0), M_Total = c(481232637, 
                                                                                                                                          405717790, 459350673, 353783075, 496680843, 408862451)), row.names = c(NA, 
                                                                                                                                                                                                                 6L), class = "data.frame")

Yes, Here is dput(head(metab))

I get this error when I try and run your app:

Warning in if (multiple) selectTag$attribs$multiple <- "multiple" :
the condition has length > 1 and only the first element will be used
Error in if (multiple) selectTag$attribs$multiple <- "multiple" :
argument is not interpretable as logical

This is a stacked bar plot with the data you have provided:

library(tidyverse)
df <- metab %>% 
  select(-M_Total) %>% 
  pivot_longer(-(Replicate:Timepoint), names_to = "category", values_to = "value")

ggplot(df, aes(Timepoint, value, fill = category)) +
  geom_bar(position="stack", stat="identity")

see: Grouped, stacked and percent stacked barplot in ggplot2 – the R Graph Gallery

Thank you, it works now. But did not update by changing the inputs.
How I can put these three input inside the staked code?
input$var (has 5 method, M0 , ... , M5)
input$Group (has two DM , FM)
input$Timepoint (has 7)

That's the result. Thank you.

Not sure. You would need to show your updated code. But it would be something like this based on your original code.

# this dataset gets filtered
ggplot_data <- data %>% 
  filter(Timepoint == input$Timepoint,
         Group == input$Group) 

# then pass it in as the data argument in the ggplot

This is the code

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      p(style = "font-family:Impact",
        a("metab Expectation")),
      
      selectInput("var",label="Choose an method",choice=c("M0",
                                                                "M1",
                                                                "M2",
                                                                "M3",
                                                                "M4",
                                                                "M_Total"), selectize=FALSE),
      selectInput(inputId = "Group", label = strong("Group"),
                  choices = unique(Aspartate$Group),
                  selected = "DM"),
      
      selectInput(inputId = "Timepoint", label = strong("Timepoint"),
                  choices = unique(Aspartate$Timepoint),
                  selected = 30),
    ),
    column(7, offset = 4,
           h4("Median of selected method:"),
           verbatimTextOutput("median"),
           h4("Median of method proportion:"),
           verbatimTextOutput("mediantotal"),
           plotOutput("box")
    )
  )
)



server <- function(input,output){
  
  output$box <- renderPlot({
    library(tidyverse)
    df <- Aspartate %>% 
      select(-M_Total) %>% 
      pivot_longer(-(Replicate:Timepoint), names_to = "category", values_to = "value")
    
    ggplot(df, aes(Timepoint, value, fill = category)) +
      geom_bar(position="stack", stat="identity")
  })
  
  output$mediantotal <- renderPrint({ 
    median(Aspartate[Aspartate$Group == input$Group & Aspartate$Timepoint == input$Timepoint,
                     which(colnames(Aspartate)==input$var)]) / 
      median(Aspartate[Aspartate$Group == input$Group & Aspartate$Timepoint == input$Timepoint,
                       colnames(Aspartate)== "M_Total"])
  })
  output$median <- renderPrint({ 
    median(Aspartate[Aspartate$Group == input$Group & Aspartate$Timepoint == input$Timepoint,
                     which(colnames(Aspartate)==input$var)])
  })
}

shinyApp(ui = ui, server = server)

This is the updated code.

Try this perhaps? I think it is what you want, though can't tell as there is not enough data to test it properly.

I moved this to the top:

library(shiny)
library(tidyverse)

And changed this:

output$box <- renderPlot({
    
    df <- Aspartate %>% 
      select(-M_Total) %>% 
      pivot_longer(-(Replicate:Timepoint), names_to = "category", values_to = "value") %>% 
      filter(Group == input$Group,
             Timepoint == input$Timepoint)
    
    ggplot(df, aes(Timepoint, value, fill = category)) +
      geom_bar(position="stack", stat="identity")
  })

Thank you William!
It connected to the data, Just it needs to be connected to the input$var , "var" is not connected!

Do you have any idea where I can add input$var?

I really appreciate your help.

But input$var controls the Method and that is what you have stacked. What do you want it to do?

You are right! Thank you!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.