Intro Thoughts

Status Quo

library(tidyverse)

Experiment

library(tidyverse)

compute_group_xy_means <- function(data, scales){
  
  data |>
    summarise(x = mean(x),
              y = mean(y),
              label = paste0("(", round(x),",", (round(y)), ")"))
  
}

StatXymeans <- ggproto("StatXymeans", 
                       Stat,
                       compute_group = compute_group_xy_means)

ggplot(cars) +
  aes(speed, dist) + 
  geom_point() + 
  layer(stat = StatXymeans,
        geom = GeomText,
        position = "identity",
        params = list(nudge_x = 3, color = "red") 
        )
## Warning: Ignoring unknown parameters: `nudge_x`

Adding nudges yourself

compute_group_xy_means2 <- function(data, scales, nudge_x = 0, nudge_y = 0){
  
  data |>
    summarise(x = mean(x) + nudge_x,
              y = mean(y) + nudge_y,
              label = paste0("(", round(x),",", (round(y)), ")"))
  
}

# updating compute
StatXymeans$compute_group <- compute_group_xy_means2

ggplot(cars) +
  aes(speed, dist) + 
  geom_point() + 
  layer(stat = StatXymeans,
        geom = GeomText,
        position = "identity",
        params = list(nudge_x = 10, color = "red") 
        )

Closing remarks, Other Relevant Work, Caveats