将自己的 ggplot 主题保存在包中并记录下来

drm*_*iod 1 r ggplot2

当我创建图表时,我一直对 ggplot 进行一些自定义更改。我想在我的包中提供这个主题,但我不知道如何保存和记录它。它既不是数据集也不是函数。这样做的首选方法是什么?

my_theme <- theme_bw() + theme(plot.title=element_text(vjust=1))
ggplot(mtcars, aes(mpg,cyl)) + geom_point() + ggtitle('test') + my_theme
Run Code Online (Sandbox Code Playgroud)

所以我想添加my_theme到文档中。

drm*_*iod 6

我刚刚通过观察意识到theme_bw我可以用同样的方式做到这一点:

my_theme <- function (base_size = 12, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace% 
    theme(plot.title=element_text(vjust=1))
}

ggplot(mtcars, aes(mpg,cyl)) + geom_point() + ggtitle('test') + my_theme()
Run Code Online (Sandbox Code Playgroud)

这样我就可以记录我的功能......