简体   繁体   中英

R Shiny - How to use the “melt” function (reshape2 package) to create a stacked barplot

thanks to your answers, I managed to make a barplot that reacts according to the time unit (Week, Month, Year) and agregates data by time unit (the link is here) : R Shiny - How to create a barplot that reacts according to the time unit (Week, Month, Year) and agregates data by time unit

Then, I wish to make a stacked barplot with two variables. For it, I generate the follow data frame with two variables (ie in my example: Imported_cases and Autochthonous_cases) and I apply the “melt” function. The UI is here :

library(shiny)
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(reshape2)

Disease <- data.frame(
  Date = seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),
  Imported_cases = rep(1),Autochtonous_cases=rep(2))
Disease <- Disease %>% mutate(
  Week = format(Date, "%Y-%m-%U"),
  Month = format(Date, "%Y-%m"), Year = format(Date, "%Y"))
Disease<- melt(Disease, id = c("Date","Week","Month","Year"), 
               measured = c("Imported_cases", "Autochtonous_cases"))
print(head(Disease))

ui <- fluidPage(
      dateRangeInput("daterange", "Choice the date",
                     start = min(Disease$Date),
                     end   = max(Disease$Date),
                     min   = min(Disease$Date),
                     max   = max(Disease$Date),
                     separator = " - ", format = "dd/mm/yy",
                     startview = 'Month', language = 'fr', weekstart = 1),
      selectInput(inputId = 'Time_unit',
                  label = 'Time_unit',
                  choices = c('Week', 'Month', 'Year'),
                  selected = 'Month'),
                  plotOutput("Disease"))

When I run my server, R Shiny displays : Error object 'variable' not found. You find bellow the server code :

server <- function(input, output) {
dateRangeInput <- reactive({
dataset <- subset(
  Disease, Date >= input$daterange[1] & Date <= input$daterange[2])
dataset
})

selectInput = reactive({
dataset <- dateRangeInput() %>% group_by_(input$Time_unit) %>% 
  summarise(Sum = sum(value))
dataset
})

output$Disease <-renderPlot({
                ggplot(data=selectInput(), 
                aes_string(x = input$Time_unit, y = "Sum", 
                           fill = "variable"))  + 
                geom_bar(stat = "identity")
                })

}
shinyApp (ui = ui, server = server)

I don't know if the problem is the code of selectInput or the code of output$Disease . I don't understand why Shiny doesn't find "variable" (cf. print(head(Disease)). Thank you for your help (I hope to be clear).

Hier is code which is going to work and create the stacked bar plot:

library(shiny) 
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)
library(reshape2)

Disease<-data.frame(Date=seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),Cases=rep(1),Autochtonous_cases=rep(2))
Disease <- Disease %>% mutate(Week = format(Date, "%Y-%m-%U"),Month = format(Date, "%Y-%m"), Year = format(Date, "%Y"))
Disease<-melt(Disease,id=c("Date","Week","Month","Year")) # just id


ui <- fluidPage(
  dateRangeInput("daterange", "Choice the date",
                 start = min(Disease$Date),
                 end = max(Disease$Date),
                 min = min(Disease$Date),
                 max = max(Disease$Date),
                 separator = " - ", format = "dd/mm/yy",
                 startview = 'Month', language = 'fr', weekstart = 1),
  selectInput(inputId = 'Time_unit',
              label='Time_unit',
              choices=c('Week','Month','Year'),
              selected='Month'),
  plotOutput("Disease"))


server <- function(input, output) {
  dateRangeInput<-reactive({
    dataset <- subset(Disease, Date >= input$daterange[1] & Date <= input$daterange[2])
    dataset
  })
  selectInput= reactive({
    dataset <- dateRangeInput() %>% group_by_(input$Time_unit,"variable") %>% summarise(Sum = sum(value)) #I have added here grouping as variable
    print(head(dataset))
    dataset
  })

  output$Disease <-renderPlot({
    ggplot(data=selectInput(), aes_string(x=input$Time_unit,y="Sum", fill = "variable"))  + geom_bar(stat="identity") + 
      labs(title="Disease", y ="Number of cases") +
      theme_classic() + 
      theme(plot.title = element_text(hjust = 0.5))
  })

}
shinyApp (ui = ui, server = server)

I guess this is what You are looking for. You had small mistakes in melt function, setting up only id variables is fair enough, second thing is to consider the created variable column in group_by_ (as You wanna get the count of cases and autochtonous cases), and last is using variable as an fill argument in ggplot .

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