How is ggplot2 plus defined?

+usually throws out non-numeric argument to binary operatorif there is anything other than a number. But it works with ggplot2, inserting the plot together. At the same time, it retains its usual function outside the context of ggplot2 (for example, as an arithmetic or formula operator), therefore its version of ggplot2 does not contradict any of them.

I want to understand how to +make me act like that. Looking through the ggplot2 github repo, I found function definitions for +.ggand %+%, but that did not make me more clear.

I would be pleased with the pointer to the code in the ggplot2 package that does this, or a generalized explanation of how this is done.

+4
source share
1

+.gg, :

> ggplot2:::`+.gg`
function (e1, e2) 
{
  e2name <- deparse(substitute(e2))
  if (is.theme(e1)) 
    add_theme(e1, e2, e2name)
  else if (is.ggplot(e1)) 
    add_ggplot(e1, e2, e2name)
}

, add_theme, add_ggplot, ggplot2:::add_ggplot. - , - "" , .

, R "" "+" gg, (- S3 classes). ggplot2 GitHub, ggproto.R, , , ggplot2 .

, ?

+5

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


All Articles