I am using checkboxGroupInput for data filtering and am using ggplot to plot line graph. The plot shows up if one out of three options is selected, however if two or three checkboxes are selected it gives an error "Aesthetics must be either length 1 or the same as the data (10): group and colour". Also the plot doesn't update when joining_date is updated.
mydata<-structure(list(employee = c("Mary", "rob",
"smary", "rob", "Abe", "Abe"
), joining_date = structure(c(17869, 17862, 17865, 17848,
17862, 17848), class = "Date"), batch___A_2019 = c(1, 1,
1, 1, 1, 0), batches___A_2020 = c(0, 0, 0, 0, 0, 1), batch___B_2020 = c(0,
0, 0, 0, 0, 0), batch___B_2023 = c(0, 0, 0, 0, 0, 0)), row.names = c(NA,
6L), class = "data.frame")
Filter data:
timeseries <- reactive({
req(input$employee)
req(input$test)
req(input$Dates)
mydata %>%
filter(
employee == input$employee,
if_any(matches(str_c(
'batch___', tolower(input$test)
)), ~
.x == 1),
joining_date >= input$Dates[1] &
joining_date <= input$Dates[2]
) %>%
group_by(employee, month = lubridate::floor_date(joining_date, 'month')) %>%
summarize(Total = n(), .groups = "drop") %>%
complete(month = seq.Date(min(month), max(month), by="month"), employee) %>%
replace(is.na(.), 0)
})
Code to generate the plot:
output$timeseriesplot <- renderPlot({
timeseries() %>%
ggplot() +
geom_line(aes(x = month, y = Total, group=input$test, color=factor(input$test))) +
scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
theme(axis.text.x=element_text(angle=50, hjust=1)) +
ylab("Total")
})