Step 00: Press play on ‘Extending your Ability to Extend’ https://www.youtube.com/watch?v=uj7A3i2fi54

Verbatim approach

create_cicle
create_cicle
library(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.2.9000     ✔ 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
create_circle <- function(data, n){

  angles <- seq(from = 0, 
                to = 2 * pi,
                length.out = n + 1)

  data.frame(
    x = cos(angles) * data$r + data$x0,
    y = sin(angles) * data$r + data$y0,
    data
  )

}
StatCirc
StatCirc
StatCircle <- ggproto(`_class` = "StatCircle", 
                      `_inherit` = Stat, 
                      setup_data = function(data, params) {
  
    if (data$group[1] == -1) {
      nrows <- nrow(data)
      data$group <- seq_len(nrows)
    }
  
    data  # return data with a group variable

},
                      compute_group = function(data, scales, n = 5){create_circle(data, n = n)},
                      required_aes = c("x0", "y0", "r")
                      )

using usingfacet

circles <- data.frame(x0 = c(-5,5), y0 = c(5, -5),
                      r = c(5, 4), class = c("A", "B"))

ggplot(circles) + 
  geom_polygon(stat = StatCircle,
               aes(x0 = x0, y0 = y0, 
                   r = r, fill = class))
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded

usingfacet
usingfacet
ggplot(circles) + 
  geom_polygon(stat = StatCircle,
               aes(x0 = x0, y0 = y0, 
                   r = r, fill = class)) + 
  facet_wrap(~ class)
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded
## Warning in data.frame(x = cos(angles) * data$r + data$x0, y = sin(angles) * :
## row names were found from a short variable and have been discarded

ggplot(cars |> mutate(row = row_number())) + 
  
  aes(x0 = speed, y0 = dist, r = 5) + 
  geom_polygon(stat = StatCircle) + 
  aes(fill = speed > 15) + 
  facet_wrap(~ speed > 15)
## Warning in cos(angles) * data$r: longer object length is not a multiple of
## shorter object length
## Warning in sin(angles) * data$r: longer object length is not a multiple of
## shorter object length

last_plot() +
  aes(group = row) + 
  geom_point(aes(x = speed, y = dist))


knitr::knit_exit()