Easy geom recipes is open for comment. Please feel free to be in touch about issues (report an issue/edit this page/email/discuss). Recipe #1 is close to final form, and I will use that as a model - incorporating your feedback.
This tutorial is intended for individuals who already have a working knowledge of the grammar of ggplot2, but may like to build a richer vocabulary for themselves via the Stat extension mechanism.
Preview
Our recipes take the form:
Step 0. Get the job done with βbaseβ ggplot2. Itβs a good idea to clarify what needs to happen without getting into the extension architecture
Step 1. Write a computation function. Wrap the necessary computation into a function that your target geom_*() function will perform. We focus on βcompute_groupβ computation only in this tutorial.
Step 2. Define a ggproto object. ggproto objects allow your extension to work together with base ggplot2 functions! Youβll use the computation function from step 1 to help define it.
Step 3. Write your geom function! Youβre ready to write your function. You will incorporate the ggproto from step 2 and also define which more primitive geom (point, text, segment etc) you want other behaviors to inherit from.
Step 4. Test/Enjoy! Take your new geom for a spin! Check out group-wise computation behavior!
Below, youβll see a completely worked example (example recipes) and then a invitation to build a related target geom_*().
Example recipe #2: geom_label_id()
Step 0: use base ggplot2 to get the job done
# step 0.alibrary(tidyverse)
ββ Attaching core tidyverse packages ββββββββββββββββββββββββ tidyverse 2.0.0 ββ
β dplyr 1.1.4 β readr 2.1.5
β forcats 1.0.0 β stringr 1.5.1
β ggplot2 3.5.1 β tibble 3.2.1
β lubridate 1.9.3 β tidyr 1.3.1
β purrr 1.0.2
ββ Conflicts ββββββββββββββββββββββββββββββββββββββββββ tidyverse_conflicts() ββ
β dplyr::filter() masks stats::filter()
β dplyr::lag() masks stats::lag()
βΉ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
label x y PANEL group colour fill size angle hjust vjust alpha family
1 1 4 2 1 -1 black white 3.88 0 1.2 0.5 NA
2 2 4 10 1 -1 black white 3.88 0 1.2 0.5 NA
3 3 7 4 1 -1 black white 3.88 0 1.2 0.5 NA
4 4 7 22 1 -1 black white 3.88 0 1.2 0.5 NA
5 5 8 16 1 -1 black white 3.88 0 1.2 0.5 NA
6 6 9 10 1 -1 black white 3.88 0 1.2 0.5 NA
fontface lineheight
1 1 1.2
2 1 1.2
3 1 1.2
4 1 1.2
5 1 1.2
6 1 1.2
Step 1: computation
# you won't use the scales argument, but ggplot will latercompute_group_row_number <-function(data, scales){ data %>%# add an additional column called label# the geom we inherit from requires the label aestheticmutate(label =1:n())}# step 1b test the computation function cars %>%# input must have required aesthetic inputs as columnsrename(x = speed, y = dist) %>%compute_group_row_number() %>%head()