[英]R Shiny - How to create a barplot that reacts according to the time unit (Week, Month, Year) and aggregates data by time unit
I generate a dataframe about a disease with the following variables: 我使用以下变量生成有关疾病的数据框:
My ui is here: 我的用户界面在这里:
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 (ie 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: 如果选择周,月或年,您将在下面找到想要获得的条形图:
by Month: https://i.stack.imgur.com/wyWoL.png 每月发布: https : //i.stack.imgur.com/wyWoL.png
by Week: https://i.stack.imgur.com/jAWJq.png 按周: https : //i.stack.imgur.com/jAWJq.png
Sorry for "by Year", I can't to post more than 2 links 抱歉,“按年份”发布的链接不能超过2个
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)
Plot is reactive to the Time_unit
input, You just need to do small adjustment with the axis text and thats it. 绘图对Time_unit
输入有反应,您只需要对轴文本进行少量调整即可。
PS Your second link to the image PS您的第二张图片链接
by Week 按周
is not working --> getting an empty space. 无法正常工作->获得一个空白空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.