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

Added fable/feast support #2045

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,41 @@ to_basic.GeomQuantile <- function(data, prestats_data, layout, params, p, ...){
dat
}

to_basic.GeomHiloLinerange <- function(data, ...){
prefix_class(data, "GeomPath")
}

transform_hiloribbon <- function(data) {
data <- data[order(data$x), ]
data$hilo <- NULL

data$x_plotlyDomain <- as.character(data$x_plotlyDomain)

maximum_lev <- max(data$level) + 1

data$alpha <- (maximum_lev * (maximum_lev - data$level) - 1 )/ maximum_lev**3
data$colour <- data$alpha

unused_aes <- ! names(data) %in% c("x", "y", "ymin", "ymax")

row_number <- nrow(data)

data_rev <- data[row_number:1L, ]
structure(rbind(
cbind(x = data$x, y = data$ymin, data[unused_aes]),
cbind(x = data$x[row_number], y = data$ymin[row_number], data[row_number, unused_aes]),
cbind(x = data_rev$x, y = data_rev$ymax, data_rev[unused_aes])
), class = class(data))
}
Comment on lines +620 to +641
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come to think of it, I think most of this as well as to_basic.GeomAlluvium() could be replaced by ribbon_dat()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same code with minor changes.


to_basic.GeomHiloRibbon <- function(data, ...){
prefix_class(transform_hiloribbon(data), "GeomPolygon")
}

#' @export
to_basic.data.frame <- function(data, prestats_data, layout, params, p, ...) {
prefix_class(data, "GeomPath")
}
Comment on lines +648 to +650
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this doesn't seem right, do you have an example where this becomes relevant?

Copy link
Contributor Author

@moutikabdessabour moutikabdessabour Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually a trick because the autoplot returns only a data.frame and GeomHilo... this results in only drawing the predictions without the line.

#' @export
to_basic.default <- function(data, prestats_data, layout, params, p, ...) {
data
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/fabletools/autoplot-fable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/testthat/test-fabletools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("mimics the autoplot output", {
# taken from https://fable.tidyverts.org/articles/fable.html
data <- tsibble::tourism %>%
filter(Region == "Melbourne") %>%
`[`(, c("Quarter", "Trips", "Region")) %>%
distinct(Quarter, .keep_all = TRUE) %>%
as_tsibble(key = Region)
p <- data %>%
model(
ets = ETS(Trips ~ trend("A")),
) %>%
forecast(h = "5 years") %>%
autoplot(data)
expect_doppelganger(ggplotly(p), "autoplot-fable")
})