How can I override the ggplot function in R?

I use the command theme_bwin almost all of my R-graphs, so I thought about overriding the ggplot function as follows:

# Override the ggplot function to use theme_bw by default
ggplot <- function(...) {
  ggplot(...) + theme_bw()
}

However, when I do this, the interpreter complains saying

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

Is it possible to indicate that ggplot inside the function should be the original version of ggplot, and not the one I just wrote?

+4
source share
2 answers

Use the operator ::to indicate that you want to call the version of the function that is in the package ggplot2, and not just the version you just created in the global workspace. that is, something like

ggplot <- function(...) {
  ggplot2::ggplot(...) + theme_bw()
}

should work (although I have not tested it!)

theme_bw(). , theme_set() , .

library("ggplot2"); theme_set(theme_bw())

, , /, .

+10

Rprofile.site. ( Windows: C:\Program Files\R\R-n.n.n\etc).

:

.First <- function(){
ggplot2::theme_set(theme_bw())
}

, .First .

0

Source: https://habr.com/ru/post/1598310/


All Articles