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)