Closed long39ng closed 3 years ago
This would be great to have, but will be complicated to add due to the limited nature of available temporal classes.
My suggestion at this stage is to build your own plot, here's some example code:
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
tsibbledata::vic_elec %>%
mutate(grp = floor_date(Time, "week", week_start = 1)) %>%
ggplot(aes(x = as.POSIXct(as.numeric(Time) - as.numeric(grp), origin = "1973-01-01", tz = tz(Time)),
y = Demand, colour = grp, group = grp)) +
geom_line() +
scale_x_datetime(date_breaks = "day", date_labels = "%a")
Created on 2020-07-06 by the reprex package (v0.3.0)
The gg_season()
package handles timezones (which is why the day indicators above are misplaced), and attempts to automatically detect/build appropriate groups for a given seasonal period.
If you add the viridis scale to the code above, you'll find the transform functions for dates to plot numbers cannot be inverted by default. There is likely some way around this (and if you find one whilst keeping the time class, please let me know!).
My workaround for now is to simply add an argument that specifies the colour palette, e.g., pal = scales::hue_pal()(9)
, to the gg_season()
function definition
and set the colours
argument here
https://github.com/tidyverts/feasts/blob/55b0415c4478ec9de7e4852d37708dc4613ac2c9/R/graphics.R#L264
to colours = pal
.
The result:
tsibbledata::vic_elec %>%
gg_season(Demand, period = "week", pal = scales::viridis_pal()(9))
Unfortunately, I'm not at all experienced with package development to know if this change would cause any problems.
That would also work.
I'm apprehensive to add this though, as the more intuitive interface is to add scale_colour_*
which you first tried. This interface is hopefully possible in the future, which would justify the removal of the pal
argument.
However that being said, retaining the argument is entirely possible for backwards compatibility. Would you like to try and write a pull request?
I would also much prefer to be able to change the colours using the ggplot2 framework. Adding an extra function argument as a quick fix does not seem to fit very well with the tidyverse philosophy.
This might soon make it easier to specify the colour scale without conflicting with the labels generated by gg_season()
.
Thanks for the pull request. It should be possible within the ggplot framework, but it would require custom classes for the time bins. I hope to (or hope someone else will) work on these time structures within the next year.
Resolved by #110
Currently, the default colour palette for time series in
gg_season()
is set toscales::hue_pal()(9)
-- without the option to specify an alternative colour scale. Setting a new colour scale, e.g., using ggplot2 scale functions, would lose the labels.