Error message when I try to create an epicurve

Thank you for posting! Here is an outline of an effective post:

I get the following error message when I try to create my epicurve.

Error in unsupported_date_time(time) :
Unsupported date-time class ‘character’

Please see my R code below.

# Loads packages 
pacman::p_load(
  rio,          # for importing data
  here,         # for locating files
  janitor,      # for data cleaning  
  lubridate,    # for date cleaning
  epikit,       # for easy inline code
  tsibble,      # for epiweeks and other time series analyses
  datapasta,
  reprex,
  tidyverse     # for data management and visualization
)

# data
combined <- data.frame(
  stringsAsFactors = FALSE,
  date_report = c("2020-09-09","2020-07-25",
                  "2020-07-02","2020-11-05","2020-09-26","2020-09-26",
                  "2020-07-07","2020-06-06","2020-06-06","2020-08-08",
                  "2020-07-24","2021-02-11","2020-09-15","2020-09-15",
                  "2020-11-30","2021-04-13","2021-04-13","2021-04-22",
                  "2020-07-14","2020-11-19","2020-11-19","2020-12-13",
                  "2020-12-13","2020-12-15","2020-07-13"),
  city = c("Atlanta","Union City",
           "Atlanta","Atlanta","Atlanta","East Point","Alpharetta",
           "Atlanta","East Point","Alpharetta","Atlanta","Atlanta",
           "Atlanta","College Park","Union City","Atlanta",
           "College Park","Atlanta","Roswell","Atlanta",
           "College Park","Atlanta","Hapeville","Atlanta","Atlanta"),
  population = c(498715,26830,498715,498715,
                 498715,38358,65818,498715,38358,65818,498715,
                 498715,498715,13930,26830,498715,13930,498715,92833,
                 498715,13930,498715,6553,498715,498715))
  
# Graphs  -----------------------------------------------------------------
# Create a Weekly Epidemic Curve by City 

# Define and save the vector (monthly)
covid_months <- seq.Date(from = floor_date(min(combined$date_report, na.rm=T), unit = "month"),
                        to =   ceiling_date(max(combined$date_report, na.rm=T), unit = "month"),
                        by =   "month")
#> Error in unsupported_date_time(time): Unsupported date-time class 'character'

# Weekly reported cases epicurve 
ggplot(data = combined, aes(x = date_report, fill=city)) +
  geom_histogram(
    breaks = covid_months,
    closed = "left") +
  scale_x_date(
    labels = scales::label_date_short(),
    date_breaks = "weeks") +
  labs(title = "COVID-19 epidemic",
       x = "Date of report", 
       y = "Number of cases",
       fill = "City") +
  theme_bw()
#> Error in eval(expr, envir, enclos): object 'covid_months' not found

Created on 2024-03-04 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.utf8
#>  ctype    English_United Kingdom.utf8
#>  tz       Europe/London
#>  date     2024-03-04
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package           * version date (UTC) lib source
#>  anytime             0.3.9   2020-08-27 [1] CRAN (R 4.3.2)
#>  apyramid          * 0.1.3   2023-02-14 [1] CRAN (R 4.3.2)
#>  askpass             1.2.0   2023-09-03 [1] CRAN (R 4.3.2)
#>  broom.helpers       1.14.0  2023-08-07 [1] CRAN (R 4.3.2)
#>  class               7.3-22  2023-05-03 [2] CRAN (R 4.3.1)
#>  classInt            0.4-10  2023-09-05 [1] CRAN (R 4.3.2)
#>  cli                 3.6.1   2023-03-23 [1] CRAN (R 4.3.2)
#>  colorspace          2.1-0   2023-01-23 [1] CRAN (R 4.3.2)
#>  crayon              1.5.2   2022-09-29 [1] CRAN (R 4.3.2)
#>  crul                1.4.0   2023-05-17 [1] CRAN (R 4.3.2)
#>  curl                5.1.0   2023-10-02 [1] CRAN (R 4.3.2)
#>  data.table          1.14.8  2023-02-17 [1] CRAN (R 4.3.2)
#>  datapasta         * 3.1.0   2020-01-17 [1] CRAN (R 4.3.2)
#>  DBI                 1.1.3   2022-06-18 [1] CRAN (R 4.3.2)
#>  digest              0.6.33  2023-07-07 [1] CRAN (R 4.3.2)
#>  dplyr             * 1.1.3   2023-09-03 [1] CRAN (R 4.3.2)
#>  e1071               1.7-13  2023-02-01 [1] CRAN (R 4.3.2)
#>  ellipsis            0.3.2   2021-04-29 [1] CRAN (R 4.3.2)
#>  epikit            * 0.1.6   2024-01-23 [1] CRAN (R 4.3.2)
#>  evaluate            0.23    2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi               1.0.5   2023-10-08 [1] CRAN (R 4.3.2)
#>  fastmap             1.1.1   2023-02-24 [1] CRAN (R 4.3.2)
#>  flextable         * 0.9.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  fontBitstreamVera   0.1.1   2017-02-01 [1] CRAN (R 4.3.1)
#>  fontLiberation      0.1.0   2016-10-15 [1] CRAN (R 4.3.1)
#>  fontquiver          0.2.1   2017-02-01 [1] CRAN (R 4.3.2)
#>  forcats           * 1.0.0   2023-01-29 [1] CRAN (R 4.3.2)
#>  fs                  1.6.3   2023-07-20 [1] CRAN (R 4.3.2)
#>  gdtools             0.3.5   2023-12-09 [1] CRAN (R 4.3.2)
#>  generics            0.1.3   2022-07-05 [1] CRAN (R 4.3.2)
#>  gfonts              0.2.0   2023-01-08 [1] CRAN (R 4.3.2)
#>  ggExtra           * 0.10.1  2023-08-21 [1] CRAN (R 4.3.2)
#>  gghighlight       * 0.4.0   2022-10-16 [1] CRAN (R 4.3.2)
#>  ggplot2           * 3.4.4   2023-10-12 [1] CRAN (R 4.3.2)
#>  glue                1.6.2   2022-02-24 [1] CRAN (R 4.3.2)
#>  gridExtra           2.3     2017-09-09 [1] CRAN (R 4.3.2)
#>  gt                  0.10.0  2023-10-07 [1] CRAN (R 4.3.2)
#>  gtable              0.3.4   2023-08-21 [1] CRAN (R 4.3.2)
#>  gtsummary         * 1.7.2   2023-07-15 [1] CRAN (R 4.3.2)
#>  here              * 1.0.1   2020-12-13 [1] CRAN (R 4.3.2)
#>  hms                 1.1.3   2023-03-21 [1] CRAN (R 4.3.2)
#>  htmltools           0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
#>  httpcode            0.3.0   2020-04-10 [1] CRAN (R 4.3.2)
#>  httpuv              1.6.12  2023-10-23 [1] CRAN (R 4.3.2)
#>  janitor           * 2.2.0   2023-02-02 [1] CRAN (R 4.3.2)
#>  jsonlite            1.8.7   2023-06-29 [1] CRAN (R 4.3.2)
#>  KernSmooth          2.23-21 2023-05-03 [2] CRAN (R 4.3.1)
#>  knitr               1.45    2023-10-30 [1] CRAN (R 4.3.2)
#>  later               1.3.1   2023-05-02 [1] CRAN (R 4.3.2)
#>  lifecycle           1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
#>  lubridate         * 1.9.3   2023-09-27 [1] CRAN (R 4.3.2)
#>  magrittr            2.0.3   2022-03-30 [1] CRAN (R 4.3.2)
#>  mime                0.12    2021-09-28 [1] CRAN (R 4.3.1)
#>  miniUI              0.1.1.1 2018-05-18 [1] CRAN (R 4.3.2)
#>  munsell             0.5.0   2018-06-12 [1] CRAN (R 4.3.2)
#>  officer           * 0.6.3   2023-10-22 [1] CRAN (R 4.3.2)
#>  openssl             2.1.1   2023-09-25 [1] CRAN (R 4.3.2)
#>  pacman              0.5.1   2019-03-11 [1] CRAN (R 4.3.2)
#>  pillar              1.9.0   2023-03-22 [1] CRAN (R 4.3.2)
#>  pkgconfig           2.0.3   2019-09-22 [1] CRAN (R 4.3.2)
#>  promises            1.2.1   2023-08-10 [1] CRAN (R 4.3.2)
#>  proxy               0.4-27  2022-06-09 [1] CRAN (R 4.3.2)
#>  purrr             * 1.0.2   2023-08-10 [1] CRAN (R 4.3.2)
#>  R6                  2.5.1   2021-08-19 [1] CRAN (R 4.3.2)
#>  ragg                1.2.6   2023-10-10 [1] CRAN (R 4.3.2)
#>  RColorBrewer      * 1.1-3   2022-04-03 [1] CRAN (R 4.3.1)
#>  Rcpp                1.0.11  2023-07-06 [1] CRAN (R 4.3.2)
#>  readr             * 2.1.4   2023-02-10 [1] CRAN (R 4.3.2)
#>  reprex            * 2.0.2   2022-08-17 [1] CRAN (R 4.3.2)
#>  rio               * 1.0.1   2023-09-19 [1] CRAN (R 4.3.2)
#>  rlang               1.1.2   2023-11-04 [1] CRAN (R 4.3.2)
#>  rmarkdown           2.25    2023-09-18 [1] CRAN (R 4.3.2)
#>  rprojroot           2.0.4   2023-11-05 [1] CRAN (R 4.3.2)
#>  rstudioapi          0.15.0  2023-07-07 [1] CRAN (R 4.3.2)
#>  scales            * 1.2.1   2022-08-20 [1] CRAN (R 4.3.2)
#>  sessioninfo         1.2.2   2021-12-06 [1] CRAN (R 4.3.2)
#>  sf                  1.0-14  2023-07-11 [1] CRAN (R 4.3.2)
#>  shiny               1.8.0   2023-11-17 [1] CRAN (R 4.3.2)
#>  snakecase           0.11.1  2023-08-27 [1] CRAN (R 4.3.2)
#>  stringi             1.8.1   2023-11-13 [1] CRAN (R 4.3.2)
#>  stringr           * 1.5.1   2023-11-14 [1] CRAN (R 4.3.1)
#>  systemfonts         1.0.5   2023-10-09 [1] CRAN (R 4.3.2)
#>  textshaping         0.3.7   2023-10-09 [1] CRAN (R 4.3.2)
#>  tibble            * 3.2.1   2023-03-20 [1] CRAN (R 4.3.2)
#>  tidyr             * 1.3.0   2023-01-24 [1] CRAN (R 4.3.2)
#>  tidyselect          1.2.0   2022-10-10 [1] CRAN (R 4.3.2)
#>  tidyverse         * 2.0.0   2023-02-22 [1] CRAN (R 4.3.2)
#>  timechange          0.2.0   2023-01-11 [1] CRAN (R 4.3.2)
#>  tsibble           * 1.1.3   2022-10-09 [1] CRAN (R 4.3.2)
#>  tzdb                0.4.0   2023-05-12 [1] CRAN (R 4.3.2)
#>  units               0.8-4   2023-09-13 [1] CRAN (R 4.3.2)
#>  utf8                1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  uuid                1.1-1   2023-08-17 [1] CRAN (R 4.3.1)
#>  vctrs               0.6.4   2023-10-12 [1] CRAN (R 4.3.2)
#>  viridis           * 0.6.4   2023-07-22 [1] CRAN (R 4.3.2)
#>  viridisLite       * 0.4.2   2023-05-02 [1] CRAN (R 4.3.2)
#>  withr               2.5.2   2023-10-30 [1] CRAN (R 4.3.2)
#>  xfun                0.41    2023-11-01 [1] CRAN (R 4.3.2)
#>  xml2                1.3.5   2023-07-06 [1] CRAN (R 4.3.2)
#>  xtable              1.8-4   2019-04-21 [1] CRAN (R 4.3.2)
#>  yaml                2.3.7   2023-01-23 [1] CRAN (R 4.3.2)
#>  zip                 2.3.0   2023-04-17 [1] CRAN (R 4.3.2)
#> 
#>  [1] C:/Users/mikhailia.mcintosh/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Many thanks for your help.

1 Like

Hello,

You need to convert date_reoprt in your data to have a date type in order for R to understand it’s a date, see below:

# loading packages
library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test

# creating the data
combined <- data.frame(
    stringsAsFactors = FALSE,
    date_report = c("2020-09-09","2020-07-25",
                                    "2020-07-02","2020-11-05","2020-09-26","2020-09-26",
                                    "2020-07-07","2020-06-06","2020-06-06","2020-08-08",
                                    "2020-07-24","2021-02-11","2020-09-15","2020-09-15",
                                    "2020-11-30","2021-04-13","2021-04-13","2021-04-22",
                                    "2020-07-14","2020-11-19","2020-11-19","2020-12-13",
                                    "2020-12-13","2020-12-15","2020-07-13"),
    city = c("Atlanta","Union City",
                     "Atlanta","Atlanta","Atlanta","East Point","Alpharetta",
                     "Atlanta","East Point","Alpharetta","Atlanta","Atlanta",
                     "Atlanta","College Park","Union City","Atlanta",
                     "College Park","Atlanta","Roswell","Atlanta",
                     "College Park","Atlanta","Hapeville","Atlanta","Atlanta"),
    population = c(498715,26830,498715,498715,
                                 498715,38358,65818,498715,38358,65818,498715,
                                 498715,498715,13930,26830,498715,13930,498715,92833,
                                 498715,13930,498715,6553,498715,498715))

# cleaning the data
clean_combined <- combined |>
    mutate(date_report = ymd(date_report))

# creating a vector of breakpoints for the histogram
covid_months <-
    seq.Date(
        from = floor_date(min(clean_combined$date_report, na.rm = T), unit = "month"),
        to = ceiling_date(max(clean_combined$date_report, na.rm = T), unit = "month"),
        by = "month"
    )

# creating the epicurve
ggplot(data = clean_combined, aes(x = date_report, fill = city)) +
    geom_histogram(breaks = covid_months,
                                 closed = "left") +
    scale_x_date(labels = scales::label_date_short(),
                             date_breaks = "weeks") +
    labs(title = "COVID-19 epidemic",
             x = "Date of report",
             y = "Number of cases",
             fill = "City") +
    theme_bw()

Created on 2024-03-04 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       macOS Ventura 13.6.3
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-03-04
#>  pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  curl          5.2.0   2023-12-08 [1] CRAN (R 4.3.0)
#>  digest        0.6.34  2024-01-11 [1] RSPM (R 4.3.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.0)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] RSPM (R 4.3.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.3.0)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.0)
#>  highr         0.10    2022-12-22 [1] CRAN (R 4.3.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.0)
#>  janitor     * 2.2.0   2023-02-02 [1] CRAN (R 4.3.0)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.0)
#>  labeling      0.4.3   2023-08-29 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  readr       * 2.1.5   2024-01-10 [1] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [1] RSPM (R 4.3.0)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.0)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  snakecase     0.11.1  2023-08-27 [1] CRAN (R 4.3.0)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.0)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.0)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] RSPM (R 4.3.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.3.0)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.3.0)
#>  xfun          0.42    2024-02-08 [1] RSPM (R 4.3.0)
#>  xml2          1.3.6   2023-12-04 [1] CRAN (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.3/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

You may also want to adjust your breakpoints or how R labels them, as well.

All the best,

Tim