Intro Thoughts

Status Quo

Experiment

library(tidyverse)

compute_group_xmean <- function(data, scales){
  
  data |>
    summarise(xintercept = mean(x)) 
  
}

GeomVline$required_aes
## [1] "xintercept"
StatXmean <- ggproto("StatXmean",
                     Stat,
                     compute_group = compute_group_xmean)

ggplot(cars) + 
  aes(x = speed) + 
  layer(GeomVline, 
        StatXmean, 
        position = "identity")
## Warning: The following aesthetics were dropped during statistical transformation: x.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

StatXmean$dropped_aes <- "x"

ggplot(cars) + 
  aes(x = speed) + 
  layer(GeomVline, 
        StatXmean, 
        position = "identity")

layer_data()
##   xintercept PANEL group colour linewidth linetype alpha
## 1       15.4     1    -1  black       0.5        1    NA

You can use segment, but it doesn’t draw unless there is a y var, which doesn’t seem ideal.

library(tidyverse)

compute_group_xmean2 <- function(data, scales){
  
  data |>
    summarize(x = mean(x)) |>
    mutate(xend = x,
           y = -Inf,
           yend = Inf)
  
}

GeomSegment$required_aes
## [1] "x"         "y"         "xend|yend"
StatXmean2 <- ggproto("StatXmean2",
                     Stat,
                     compute_group = compute_group_xmean2)

ggplot(cars) + 
  aes(x = speed) + 
  layer(GeomSegment, 
        StatXmean2, 
        position = "identity") + 
  geom_rug()

last_plot() + 
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

library(tidyverse)

draw_panel_xline = function(data, panel_params, coord) {

  ranges <- coord$backtransform_range(panel_params)

  data$x    <- data$x
  data$xend <- data$x
  data$y    <- ranges$y[1]
  data$yend <- ranges$y[2]

  GeomSegment$draw_panel(data, panel_params, coord)

}


GeomXline <- ggplot2::ggproto("GeomXline", ggplot2::Geom,
                     draw_panel = draw_panel_xline,
                     default_aes = ggplot2::aes(colour = "black", size = 0.5,
                                       linetype = 1, alpha = NA),
                     required_aes = "x",
                     draw_key = ggplot2::draw_key_vline
)


compute_group_xmean3 <- function(data, scales){
  
  data |>
    summarize(x = mean(x))
  
}


StatXmean3 <- ggproto("StatXmean3",
                     Stat,
                     compute_group = compute_group_xmean3)


ggplot(cars) + 
  aes(x = speed) + 
  layer(GeomXline,  #<<<<<
        StatXmean3, 
        position = "identity") + 
  geom_rug()
## Warning: Using the `size` aesthetic with geom_segment was deprecated in ggplot2 3.4.0.
## ℹ Please use the `linewidth` aesthetic instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

layer_data()
##      x PANEL group colour size linetype alpha
## 1 15.4     1    -1  black  0.5        1    NA