简体   繁体   English

R Shiny-如何使用“融化”功能(reshape2程序包)创建堆叠的条形图

[英]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 感谢您的回答,我设法制作了一个根据时间单位(周,月,年)做出反应的条形图,并按时间单位(链接在此处)聚合数据: R Shiny-如何创建根据时间单位(周,月,年)并按时间单位汇总数据

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. 为此,我生成了带有两个变量的跟随数据框(例如,在我的示例中:Imported_cases和Autochthonous_cases),然后应用“ melt”函数。 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. 当我运行服务器时,R Shiny显示:找不到错误对象“变量”。 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 . 我不知道问题是selectInput的代码还是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). 我不明白为什么Shiny找不到“变量”(请参阅​​print(head(Disease))。谢谢您的帮助(希望您能清楚地说)。

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 . 您在melt函数中犯了一些小错误,仅设置id变量就足够了,第二件事是考虑在group_by_创建的变量列(因为您想获取个案和自治个案的数量),最后是使用变量作为fill ggplot参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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