Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing adding non-integer numbers to grate objects #54

Closed
thibautjombart opened this issue Mar 1, 2021 · 4 comments
Closed

Allowing adding non-integer numbers to grate objects #54

thibautjombart opened this issue Mar 1, 2021 · 4 comments

Comments

@thibautjombart
Copy link
Contributor

thibautjombart commented Mar 1, 2021

A maybe not very frequent use case: define limits between two time intervals defined by grate, e.g. to visually delineate epochs in a graph using a vertical line.

Currently the following will error on purpose:

> as_yrwk("2021-W03") + 1
[1] "2021-W04"

> as_yrwk("2021-W03") + 1.5
Error: Can only add whole numbers to <yrwk> objects

But unsure if we want to change this or not.

@TimTaylor TimTaylor transferred this issue from reconverse/reportfactory Mar 1, 2021
@TimTaylor
Copy link
Collaborator

This is tricky as currently we transform the data to a better behaved (unit spacing between consecutive date groups) scale within the plotting function. The solution is to transform the data prior to making the class. It may actually be better to do this as the underlying representation is likely to be better for the user. Will start a new branch to explore.

@TimTaylor
Copy link
Collaborator

I think 3f1d2f4 will enable what I think your use case is with a little hacking (see below). I'm not sure we want to be any more flexible than this (i.e. allowing hacking if really needed):

library(outbreaks)
library(incidence2)
library(ggplot2)

res <- incidence(
  covid19_england_nhscalls_2020,
  date_index = date,
  interval = "1 week"
) 
res
#> An incidence2 object: 27 x 2
#> 253670 cases from 2020-W12 to 2020-W38
#> interval: 1 monday week
#> cumulative: FALSE
#> 
#>    date_index count
#>    <yrwk>     <int>
#>  1 2020-W12   12987
#>  2 2020-W13   18263
#>  3 2020-W14   14389
#>  4 2020-W15   11486
#>  5 2020-W16   10058
#>  6 2020-W17   10562
#>  7 2020-W18   11367
#>  8 2020-W19   10846
#>  9 2020-W20   10396
#> 10 2020-W21    9816
#> # … with 17 more rows
xint <- (as.integer(res$date_index[19]) + as.integer(res$date_index[20])) / 2
plot(res) + geom_vline(xintercept = xint)

Created on 2021-03-02 by the reprex package (v1.0.0)

@thibautjombart
Copy link
Contributor Author

Looks perfect to me! Agreed re the desired level of flexibility.

@TimTaylor
Copy link
Collaborator

closed with 3f1d2f4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants