19.3 Case 2: Modularity

We know that user-facing “layer” code that we add to a ggplot with + are stand-alone functions:

lm_smooth <- geom_smooth(method = "lm", formula = y ~ x)
lm_smooth
geom_smooth: na.rm = FALSE, orientation = NA, se = TRUE
stat_smooth: na.rm = FALSE, orientation = NA, se = TRUE, method = lm, formula = y ~ x
position_identity 

When we add this object to different ggplots, it materializes in different ways:

ggplot(mtcars, aes(mpg, hp)) +
  lm_smooth

ggplot(mtcars, aes(wt, disp)) +
  lm_smooth