# you won't use the scales argument, but ggplot will later
compute_group_row_number <- function(data,
                                     scales){
  data %>%
    arrange(x, y) %>% 
# add an additional column called label
# the geom we inherit from requires the label aesthetic
    mutate(id = row_number(),
           label = paste0("(list(x[", id, "], ",
                          "y[", id, "]))"))
  
  # "(list(x[1],y[1]))"
}

StatRownumber <- ggplot2::ggproto(
  `_class` = "StatRownumber",
  `_inherit` = ggplot2::Stat,
  required_aes = c("x", "y"),
  compute_group = compute_group_row_number
  )

geom_coords_notation <- function(
  mapping = NULL,
  data = NULL,
  position = "identity",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE, ...) {
  ggplot2::layer(
    stat = StatRownumber,  # proto object from Step 2
    geom = ggplot2::GeomText,  # inherit other behavior
    data = data,
    mapping = mapping,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes, 
    params = list(na.rm = na.rm,  ...)
  )
}



library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.2.0
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── 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
ggplot(data = anscombe) + 
  aes(x = x1, y = y1) + 
  geom_point() + 
  geom_coords_notation(hjust = 1.2, check_overlap = T, parse = TRUE)

geom_coords_notation0 <- function(
mapping = NULL,
data = NULL,
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE, ...) {
ggplot2::layer(
stat = StatRownumber,  # proto object from Step 2
geom = ggplot2::GeomText,  # inherit other behavior
data = data,
mapping = mapping,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
library(tidyverse)
ggplot(data = anscombe) +
aes(x = x1, y = y1) +
geom_point() +
# geom_coords_notation(hjust = 1.2, check_overlap = T, parse = TRUE) +
annotate(geom = "text", x = 1, y = 1, label = "(list(x[1],y[1]))", parse = TRUE)

ggplot(data = anscombe) +
aes(x = x1, y = y1) +
geom_point() +
geom_coords_notation0(hjust = 1.2, check_overlap = T, parse = TRUE) 

geom_coords_notation <- function(...){
  
  geom_coords_notation0(parse = TRUE, hjust = 1.2, ...)
  
}


ggplot(data = anscombe) +
aes(x = x1, y = y1) +
geom_point() +
  geom_coords_notation() 

#+
#annotate(geom = "text", x = 1, y = 1, label = "(list(x[1],y[1]))", parse = TRUE)
#ggplot(data = anscombe) +
#aes(x = x1, y = y1) +
#geom_point() +
#geom_coords_notation(hjust = 1.2, check_overlap = T)
#mapping = NULL,
#library(tidyverse)
#ggplot(data = anscombe) +
#aes(x = x1, y = y1) +
#geom_point() +
#geom_coords_notation(hjust = 1.2, check_overlap = T)