0

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?

joshpetit
  • 677
  • 6
  • 17
  • check out ?ggplot2::`%+%` and ?ggplot2:::`+.gg` – Mike Jan 19 '22 at 14:49
  • 2
    Strongly related: [How is ggplot2 plus operator defined?](https://stackoverflow.com/questions/40450904/how-is-ggplot2-plus-operator-defined) and [What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr?](https://stackoverflow.com/questions/35332861/what-is-the-difference-between-the-operator-in-ggplot2-and-the-operato) – Gregor Thomas Jan 19 '22 at 14:56

2 Answers2

7

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.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Ok awesome! That makes sense thanks a lot. I'll see if I can find the second question in stack if not I'll add another question. – joshpetit Jan 19 '22 at 15:23
4

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

hachiko
  • 671
  • 7
  • 20
  • This explains the philosophy behind the adding operator thanks! It's a new paradigm to me so will take a bit of getting used to. – joshpetit Jan 19 '22 at 16:00
  • Annoyingly, in Grammar of Graphics, this `+` means " do this *and* do that" while in Boolean software, `+` means "do this *or* do that" . Oh, well. :-) – Carl Witthoft Apr 12 '23 at 16:12