tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
528 stars 50 forks source link

Allow scale_*()$trans$breaks methods to accept the number of breaks, `n`. #216

Closed mitchelloharawild closed 3 years ago

mitchelloharawild commented 4 years ago

Most (all?) ggplot2 scale breaks allow n to determine the number of breaks

ggplot2::scale_x_date()$trans$breaks
#> function (x, n = n_default) 
#> {
#>     breaks <- pretty(x, n, ...)
#>     names(breaks) <- attr(breaks, "labels")
#>     breaks
#> }
#> <bytecode: 0x560a5d412e68>
#> <environment: 0x560a5d40f8c0>

Scales from {tsibble} do not allow this option to pretty()

tsibble::scale_x_yearmonth()$trans$breaks
#> function(x) {
#>       yearmonth(scales::breaks_pretty()(as_date(x)))
#>     }
#> <bytecode: 0x560a5e7b3828>
#> <environment: 0x560a5e7b7c18>

Created on 2020-08-25 by the reprex package (v0.3.0)

earowang commented 4 years ago

Seems that there's no the argument of n in scale_x_date*() in {ggplot2}?

library(tsibble)
library(ggplot2)

pedestrian %>% 
  dplyr::filter(Sensor == "Southern Cross Station") %>% 
  ggplot(aes(x = Date_Time, y = Count)) +
  geom_line() +
  scale_x_datetime(n = 3)

Created on 2020-08-26 by the reprex package (v0.3.0)

mitchelloharawild commented 4 years ago

I think it can get passed in from somewhere, but I forget where (possibly by changing n_default?). I use it internally for changing the number of breaks that are automatically selected.

earowang commented 4 years ago

Can you provide a scale_x_date() example that uses n?

earowang commented 3 years ago

Does the new commit resolve your issue?

tsibble::scale_x_yearmonth()$trans$breaks
#> function(x) {
#>     yearmonth(scales::breaks_pretty(n)(as_date(x)))
#>   }
#> <bytecode: 0x7fdeb2e89cd8>
#> <environment: 0x7fdeb2e86288>

Created on 2020-09-19 by the reprex package (v0.3.0)

mitchelloharawild commented 3 years ago

What's the issue with also accepting n as a parameter?

ggplot2::scale_x_date()$trans$breaks
#> function (x, n = n_default) 
#> {
#>     breaks <- pretty(x, n, ...)
#>     names(breaks) <- attr(breaks, "labels")
#>     breaks
#> }
#> <bytecode: 0x55f35ec2c068>
#> <environment: 0x55f35ec263b0>

Created on 2020-09-21 by the reprex package (v0.3.0)

earowang commented 3 years ago

The {scales} package doesn’t provide that kind of api.

Cheers, Earo

On 21 Sep 2020, at 11:01, mitchelloharawild wrote:

What's the issue with also accepting n as a parameter?

ggplot2::scale_x_date()$trans$breaks
#> function (x, n = n_default)
#> {
#>     breaks <- pretty(x, n, ...)
#>     names(breaks) <- attr(breaks, "labels")
#>     breaks
#> }
#> <bytecode: 0x55f35ec2c068>
#> <environment: 0x55f35ec263b0>

Created on 2020-09-21 by the reprex package (v0.3.0)

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/tidyverts/tsibble/issues/216#issuecomment-695847355