Hi Tim, thanks for the response. Iβm attaching a de-identified df as well as the reference dfs (5-year and 2021 cases)
For #1, Iβm still unsure how to approach the creation of a surveillance_day variable based on the date/time period I specified earlier.
For #2, I managed to create an epicurve using incidence2 package and adding 2 geom_line for each of the references (5-year daily average cases and 2021 daily cases). In order to do this, I had to make the dates for the 5-year average and 2021 the same as the 2022 surveillance period. However, Iβm unable to create a legend that will also include the color for the histogram. So far, adding scale_color_manual seems to only βgetβ the 2 geom_lines.
Thank you for your help!
# load package
pacman::p_load(lubridate, tidyverse, incidence2, scales, reprex)
# generate demo data
demo_data <- data.frame(
pno = c(3L,8L,10L,11L,18L,19L,20L,21L,23L,24L),
date_report = c("2022-12-21","2022-12-23","2022-12-23","2022-12-24","2022-12-25",
"2022-12-25","2022-12-25","2022-12-25", "2022-12-25","2022-12-25"),
time_report = c("23:50:00","16:15:28","21:25:02","02:57:25","01:51:04","02:55:10",
"04:14:59","04:37:04","05:49:53","05:51:52"),
date_inj = c("2022-12-21","2022-12-23","2022-12-23","2022-12-23","2022-12-25",
"2022-12-25","2022-12-25","2022-12-25","2022-12-22","2022-12-25"),
time_inj = c("15:00:00","14:40:00","01:00:00","14:00:00","00:30:00","00:00:00",
"00:00:00","00:20:00","14:00:00","00:30:00"))
hist_2021 <- data.frame(
date = c("2022-12-21","2022-12-22","2022-12-23","2022-12-24","2022-12-25","2022-12-26",
"2022-12-27","2022-12-28","2022-12-29","2022-12-30","2022-12-31","2023-01-01",
"2023-01-02","2023-01-03","2023-01-04","2023-01-05","2023-01-06"),
n_2021 = c(0L,1L,3L,4L,9L,4L,2L,2L,2L,10L,39L,106L,3L,1L,2L,1L,0L))
hist_5yr <- data.frame(
date = c("2022-12-21","2022-12-22","2022-12-23","2022-12-24","2022-12-25","2022-12-26",
"2022-12-27","2022-12-28","2022-12-29","2022-12-30","2022-12-31","2023-01-01",
"2023-01-02","2023-01-03","2023-01-04","2023-01-05","2023-01-06"),
n_5yr = c(3L,2L,3L,8L,11L,6L,8L,4L,4L,10L,74L,167L,9L,5L,3L,1L,0L))
# change class to dates
demo_data <- demo_data %>%
mutate(date_report = ymd(date_report),
date_inj = ymd(date_inj))
hist_2021 <- hist_2021 %>%
mutate(date = ymd(date))
hist_5yr <- hist_5yr %>%
mutate(date = ymd(date))
# create the incidence object, aggregating cases by day
epicurve <- incidence( # create incidence object
x = demo_data, # dataset
date_index = date_inj, # date column
interval = "day" # date grouping interval
)
# plot
plot(epicurve,
fill = "navyblue",
date_format = "%d",
xlab = "Date of injury",
ylab = "No. of cases",
legend = "right") +
# add a line for 5-year historical daily case average
geom_line(data = hist_5yr, mapping = aes(x = date, y = n_5yr, color = "red")) +
# add a line for 2021 daily cases
geom_line(data = hist_2021, mapping = aes(x = date, y = n_2021, color = "black")) +
# theme to remove gridlines
theme_classic() +
# add color labels (however this currently only includes the lines, not the histogram)
scale_color_manual(
values = c("black", "red", "navyblue"),
labels = c("2021", "5-year average", "2022")) +
# adjust x axis labels
scale_x_date(date_breaks = "day",
labels = label_date_short()) # minimal x-axis information
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.

Created on 2023-01-10 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> setting value
#> version R version 4.2.2 (2022-10-31)
#> os macOS Ventura 13.1
#> system aarch64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Asia/Manila
#> date 2023-01-10
#> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#>
#> β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> package * version date (UTC) lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0)
#> backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.0)
#> broom 1.0.1 2022-08-29 [1] CRAN (R 4.2.0)
#> cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.2.0)
#> cli 3.4.1 2022-09-23 [1] CRAN (R 4.2.0)
#> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0)
#> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0)
#> curl 4.3.3 2022-10-06 [1] CRAN (R 4.2.0)
#> data.table 1.14.4 2022-10-17 [1] CRAN (R 4.2.0)
#> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.2.0)
#> dbplyr 2.2.1 2022-06-27 [1] CRAN (R 4.2.0)
#> digest 0.6.30 2022-10-18 [1] CRAN (R 4.2.0)
#> dplyr * 1.0.10 2022-09-01 [1] CRAN (R 4.2.0)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0)
#> evaluate 0.18 2022-11-07 [1] CRAN (R 4.2.0)
#> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0)
#> farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.0)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0)
#> forcats * 0.5.2 2022-08-19 [1] CRAN (R 4.2.0)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0)
#> gargle 1.2.1 2022-09-08 [1] CRAN (R 4.2.0)
#> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0)
#> ggplot2 * 3.4.0 2022-11-04 [1] CRAN (R 4.2.0)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0)
#> googledrive 2.0.0 2021-07-08 [1] CRAN (R 4.2.0)
#> googlesheets4 1.0.1 2022-08-13 [1] CRAN (R 4.2.0)
#> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.0)
#> haven 2.5.1 2022-08-22 [1] CRAN (R 4.2.0)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0)
#> hms 1.1.2 2022-08-19 [1] CRAN (R 4.2.0)
#> htmltools 0.5.3 2022-07-18 [1] CRAN (R 4.2.0)
#> httr 1.4.4 2022-08-17 [1] CRAN (R 4.2.0)
#> incidence2 * 1.2.3 2021-11-07 [1] CRAN (R 4.2.0)
#> jsonlite 1.8.3 2022-10-21 [1] CRAN (R 4.2.0)
#> knitr 1.40 2022-08-24 [1] CRAN (R 4.2.0)
#> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0)
#> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0)
#> lubridate * 1.9.0 2022-11-06 [1] CRAN (R 4.2.0)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0)
#> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0)
#> modelr 0.1.10 2022-11-11 [1] CRAN (R 4.2.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0)
#> pacman 0.5.1 2019-03-11 [1] CRAN (R 4.2.0)
#> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0)
#> purrr * 0.3.5 2022-10-06 [1] CRAN (R 4.2.0)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
#> readr * 2.1.3 2022-10-01 [1] CRAN (R 4.2.0)
#> readxl 1.4.1 2022-08-17 [1] CRAN (R 4.2.0)
#> reprex * 2.0.2 2022-08-17 [1] CRAN (R 4.2.0)
#> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.0)
#> rmarkdown 2.18 2022-11-09 [1] CRAN (R 4.2.0)
#> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0)
#> rvest 1.0.3 2022-08-19 [1] CRAN (R 4.2.0)
#> scales * 1.2.1 2022-08-20 [1] CRAN (R 4.2.0)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
#> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.0)
#> stringr * 1.4.1 2022-08-20 [1] CRAN (R 4.2.0)
#> tibble * 3.1.8 2022-07-22 [1] CRAN (R 4.2.0)
#> tidyr * 1.2.1 2022-09-08 [1] CRAN (R 4.2.0)
#> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.0)
#> tidyverse * 1.3.2 2022-07-18 [1] CRAN (R 4.2.0)
#> timechange * 0.1.1 2022-11-04 [1] CRAN (R 4.2.0)
#> tzdb 0.3.0 2022-03-28 [1] CRAN (R 4.2.0)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0)
#> vctrs 0.5.0 2022-10-22 [1] CRAN (R 4.2.0)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0)
#> xfun 0.34 2022-10-18 [1] CRAN (R 4.2.0)
#> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0)
#> yaml 2.3.6 2022-10-18 [1] CRAN (R 4.2.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#>
#> ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ