R Shiny-如何创建根据时间单位(周,月,年)做出反应并按时间单位汇总数据的条形图 [英] R Shiny - How to create a barplot that reacts according to the time unit (Week, Month, Year) and aggregates data by time unit

查看:330
本文介绍了R Shiny-如何创建根据时间单位(周,月,年)做出反应并按时间单位汇总数据的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成具有以下变量的疾病数据框:

I generate a dataframe about a disease with the following variables:


  • 日期(疾病日期)

  • 病例(病例数,默认情况下,病例数为1)

  • 周(疾病周数)

  • 月(疾病月份)

  • 年份(疾病年份)。

  • Date (date of disease)
  • Cases (number of cases, by default, the number of cases is 1)
  • Week (week of disease case)
  • Month (month of disease case)
  • Year (year of disease case).

我的用户界面在这里:

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

Disease<-data.frame(Date=seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),Cases=rep(1))
Disease$Week<-as.Date(cut(Disease$Date,breaks="week",start.on.monday = TRUE))
Disease$Month<-as.Date(cut(Disease$Date,breaks="month"))
Disease$Year<-as.Date(cut(Disease$Date,breaks="year"))

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

我希望创建一个根据时间单位做出反应的条形图(即

I wish to create a barplot that reacts according to the time unit (i.e. week, month, year) and that agregates data by time unit

您将在服务器代码下方找到它(但它不起作用):

You will find below the code of the server (but it doesn't work) :

server <- function(input, output) {
       dateRangeInput<-reactive({
       dataset= subset(Disease, Date >= input$daterange[1] & Date <= 
       input$daterange[2])
       return(dataset)
       })
selectInput= reactive({
summarize(group_by(dateRangeInput(),
period = switch(input$Time_unit,
"Week"=Disease$Week,
"Month" = Disease$Month,
"Year" = Disease$Year)))
})

output$Disease <-renderPlot({
ggplot(data=selectInput(), aes_string(x="period",x="Cases"))  
+ stat_summary(fun.y = sum, geom = "bar") 
+ labs(title="Disease", y ="Number of cases")
+theme_classic() 
+theme(plot.title = element_text(hjust = 0.5))
})

}
shinyApp (ui = ui, server = server)

您如果选择周,月或年,将在下面找到我想获得的条形图:

You will find below barplots thatI would like to obtain if I choose Week or Month or Year:

https://i.stack。 imgur.com/jAWJq.png

很抱歉按年份,我不能发布2个以上的链接

Sorry for "by Year", I can't to post more than 2 links

推荐答案

您可以查看此代码,它的工作方式如您所愿:

You can check this code out, it is working how You want:

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

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

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) %>% summarise(Sum = sum(Cases))
    print(head(dataset))
    dataset
    })

  output$Disease <-renderPlot({
    ggplot(data=selectInput(), aes_string(x=input$Time_unit,y="Sum"))  + 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)

图对 Time_unit 输入,您只需要对轴文本进行简单的调整即可。

Plot is reactive to the Time_unit input, You just need to do small adjustment with the axis text and thats it.

PS您的第二个图像链接

P.S. Your second link to the image


按周

by Week

不起作用->留有空白。

is not working --> getting an empty space.

这篇关于R Shiny-如何创建根据时间单位(周,月,年)做出反应并按时间单位汇总数据的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆