繁体   English   中英

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

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

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

然后,我希望制作一个带有两个变量的堆积条形图。 为此,我生成了带有两个变量的跟随数据框(例如,在我的示例中:Imported_cases和Autochthonous_cases),然后应用“ melt”函数。 用户界面在这里:

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"))

当我运行服务器时,R Shiny显示:找不到错误对象“变量”。 您在下面找到服务器代码:

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)

我不知道问题是selectInput的代码还是output$Disease的代码。 我不明白为什么Shiny找不到“变量”(请参阅​​print(head(Disease))。谢谢您的帮助(希望您能清楚地说)。

上面的代码将起作用并创建堆积的条形图:

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)

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

暂无
暂无

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

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