What exactly is ggplot doing with the `+` operator?

I started using R recently, and have been confused with ggplot which my class is using. I'm used to the + operator just adding two outputs, but I see that in ggplot you can things such as:

ggplot(data = bechdel, aes(x = domgross_2013)) +
  geom_histogram(bins = 10, color="purple", fill="white") +
  labs(title = "Domestic Growth of Movies", x = " Domestic Growth")

Here we are adding two function calls together. What exactly is happening here? Is ggplot "overriding" the + operator (maybe like how you can override the == operator in dart?) in order to do something different? Or is it that the '+' operator means something different in R than I am used to with other programming languages?


I'll answer the first question. You should ask the second question in a separate posting.

R lets you override most operators. The easiest way to do it is using the "S3" object system. This is a very simple system where you attach an attribute named "class" to the object, and that affects how R processes some functions. (The ones this applies to are called "generic functions". There are other functions that don't pay any attention to the class.)

Each ggplot2 function returns an object with a class. You can use the class() function to get the class. For example, class(ggplot(data = "mtcars")) is a character vector containing c("gg", "ggplot"), and class(geom_histogram(bins = 10, color="purple", fill="white")) is the vector c("LayerInstance","Layer","ggproto","gg").

If you ask for methods("+") you'll see all the classes with methods defined for addition, and that includes "gg", so R will call that method to process the addition in the expression you used.


The + operator is part of the philosophy of ggplot2. It's inspired by The Grammar of Graphics, which is worth reading. Essentially, you keep creating new and new layers.

Try taking this one step at a time in your code and it should make sense!

one <- ggplot2::ggplot(data = mtcars) + 
  labs(title = "Mtcars", subtitle = "Blank Canvas")

two <- ggplot2::ggplot(data = mtcars, aes(x = mpg)) + 
  labs(title = "Mtcars", subtitle = "+ Aesthetic Mapping")

three <- ggplot2::ggplot(data = mtcars, aes(x = mpg, y = after_stat(count))) + 
  geom_histogram() 

library(patchwork)
one + two + three

enter image description here