Reprex practice - epicurve error scenario

This is a practice upload using reprex! I am trying to create an epi curve but there is an error with my x aesthetic.

# load packages
pacman::p_load(rio, janitor, tidyverse, here, datapasta)

# the data
demo_data<-data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  case_id = c("694928", "86340d", "92d002", "544bd1", "6056ba"),
  `onset date` = c("11/9/2014","10/30/2014",
                   "8/16/2014","8/29/2014","10/20/2014")
)

# epi curve
ggplot(data = demo_data, mapping = aes(x = `onset date`))+
  geom_histogram()
#> Error in `geom_histogram()`:
#> ! Problem while computing stat.
#> β„Ή Error occurred in the 1st layer.
#> Caused by error in `setup_params()`:
#> ! `stat_bin()` requires a continuous x aesthetic
#> βœ– the x aesthetic is discrete.
#> β„Ή Perhaps you want `stat="count"`?
#> Backtrace:
#>      β–†
#>   1. β”œβ”€base::tryCatch(...)
#>   2. β”‚ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. β”‚   β”œβ”€base (local) tryCatchOne(...)
#>   4. β”‚   β”‚ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. β”‚   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. β”‚     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. β”‚       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. β”œβ”€base::withCallingHandlers(...)
#>   9. β”œβ”€base::saveRDS(...)
#>  10. β”œβ”€base::do.call(...)
#>  11. β”œβ”€base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("choky-barb_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         β”œβ”€knitr:::handle_error(...)
#>  17.         β”‚ └─base::withCallingHandlers(...)
#>  18.         β”œβ”€base::withCallingHandlers(...)
#>  19.         β”œβ”€knitr:::process_group(group)
#>  20.         └─knitr:::process_group.block(group)
#>  21.           └─knitr:::call_block(x)
#>  22.             └─knitr:::block_exec(params)
#>  23.               └─knitr:::eng_r(options)
#>  24.                 β”œβ”€knitr:::in_input_dir(...)
#>  25.                 β”‚ └─knitr:::in_dir(input_dir(), expr)
#>  26.                 └─knitr (local) evaluate(...)
#>  27.                   └─evaluate::evaluate(...)
#>  28.                     └─evaluate:::evaluate_call(...)
#>  29.                       β”œβ”€evaluate (local) handle(...)
#>  30.                       β”‚ └─base::try(f, silent = TRUE)
#>  31.                       β”‚   └─base::tryCatch(...)
#>  32.                       β”‚     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  33.                       β”‚       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  34.                       β”‚         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  35.                       β”œβ”€base::withCallingHandlers(...)
#>  36.                       β”œβ”€base::withVisible(value_fun(ev$value, ev$visible))
#>  37.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  38.                         └─knitr (local) fun(x, options = options)
#>  39.                           β”œβ”€base::withVisible(knit_print(x, ...))
#>  40.                           β”œβ”€knitr::knit_print(x, ...)
#>  41.                           └─knitr:::knit_print.default(x, ...)
#>  42.                             └─evaluate (local) normal_print(x)
#>  43.                               β”œβ”€base::print(x)
#>  44.                               └─ggplot2:::print.ggplot(x)
#>  45.                                 β”œβ”€ggplot2::ggplot_build(x)
#>  46.                                 └─ggplot2:::ggplot_build.ggplot(x)
#>  47.                                   └─ggplot2:::by_layer(...)
#>  48.                                     β”œβ”€rlang::try_fetch(...)
#>  49.                                     β”‚ β”œβ”€base::tryCatch(...)
#>  50.                                     β”‚ β”‚ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  51.                                     β”‚ β”‚   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  52.                                     β”‚ β”‚     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  53.                                     β”‚ └─base::withCallingHandlers(...)
#>  54.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  55.                                       └─l$compute_statistic(d, layout)
#>  56.                                         └─ggplot2 (local) compute_statistic(..., self = self)
#>  57.                                           └─self$stat$setup_params(data, self$stat_params)
#>  58.                                             └─ggplot2 (local) setup_params(..., self = self)
#>  59.                                               └─cli::cli_abort(...)
#>  60.                                                 └─rlang::abort(...)

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

Session info
sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United Kingdom.utf8 
#> [2] LC_CTYPE=English_United Kingdom.utf8   
#> [3] LC_MONETARY=English_United Kingdom.utf8
#> [4] LC_NUMERIC=C                           
#> [5] LC_TIME=English_United Kingdom.utf8    
#> 
#> time zone: Europe/London
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] datapasta_3.1.0 here_1.0.1      lubridate_1.9.3 forcats_1.0.0  
#>  [5] stringr_1.5.0   dplyr_1.1.3     purrr_1.0.2     readr_2.1.4    
#>  [9] tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.4   tidyverse_2.0.0
#> [13] janitor_2.2.0   rio_1.0.1      
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.10.2     utf8_1.2.4        generics_0.1.3    stringi_1.7.12   
#>  [5] hms_1.1.3         digest_0.6.33     magrittr_2.0.3    evaluate_0.22    
#>  [9] grid_4.3.2        timechange_0.2.0  fastmap_1.1.1     rprojroot_2.0.4  
#> [13] R.oo_1.26.0       R.cache_0.16.0    R.utils_2.12.3    fansi_1.0.5      
#> [17] scales_1.2.1      cli_3.6.1         rlang_1.1.1       R.methodsS3_1.8.2
#> [21] munsell_0.5.0     reprex_2.0.2      withr_2.5.2       yaml_2.3.7       
#> [25] tools_4.3.2       tzdb_0.4.0        colorspace_2.1-0  pacman_0.5.1     
#> [29] vctrs_0.6.4       R6_2.5.1          lifecycle_1.0.3   snakecase_0.11.1 
#> [33] fs_1.6.3          pkgconfig_2.0.3   pillar_1.9.0      gtable_0.3.4     
#> [37] glue_1.6.2        xfun_0.40         tidyselect_1.2.0  rstudioapi_0.15.0
#> [41] knitr_1.45        htmltools_0.5.7   rmarkdown_2.25    compiler_4.3.2
1 Like

Hello,

Your onset_date has a character type which doesn’t work for a histogram as it required numeric data. To get around this, you must clean your data by converting onset_date to have a date type, 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
demo_data <- data.frame(
    stringsAsFactors = FALSE,
    check.names = FALSE,
    case_id = c("694928", "86340d", "92d002", "544bd1", "6056ba"),
    `onset date` = c(
        "11/9/2014",
        "10/30/2014",
        "8/16/2014",
        "8/29/2014",
        "10/20/2014"
    )
)

# cleaning the data
clean_demo_data <- demo_data |>
    clean_names() |>
    mutate(onset_date = mdy(onset_date))

# epi curve
ggplot(data = clean_demo_data, mapping = aes(x = onset_date))+
    geom_histogram() +
    scale_x_date(breaks = scales::breaks_pretty(), labels = scales::label_date()) +
    scale_y_continuous(breaks = scales::extended_breaks(), labels = scales::label_comma()) +
    labs(x = "\nOnset date",
             y = "Number of cases\n") +
    theme_minimal()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

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
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim