I want to have one exported function my_theme() that can be applied to different chart types (ggplot, highcharter, plotly). I tried using S3 methods but there are a few problems:
- I can only figure out how to use the pipe
|>vs the+operator for ggplot objects - I can't seem to use this in
ggplot2::theme_set(), I might create a separateuse_my_theme()function to do this
Any advice would be appreciated:
library(highcharter)
library(ggplot2)
# functions
my_theme <- function(p, ...) {
UseMethod("my_theme", p)
}
my_theme.gg <- function(p, ...) {
p +
theme_dark(...)
}
my_theme.highchart <- function(p, ...) {
p |>
hc_add_theme(hc_theme_darkunica(...))
}
# highcharter
highcharter::highcharts_demo() |>
my_theme()
# ggplot
ggplot2::qplot(mpg, wt, data = mtcars) |>
my_theme() # ^^^ how to make '+' instead?
theme_set(my_theme()) # doesn't work (and future plots will fail)
theme_set(theme_gray()) # revert back