Here a very crude stat using GeomPolygon for a horizontal cross. This is just to demonstrate a very simple and straight forward approach how to implement this. It is not a great stat, because
- the proper proportions depend on the scale (you basically need to set coord_equal),
- You need to be very specific with the groups
but it might be a good start...
library(tidyverse)
StatCross <- ggproto("StatCross", Stat,
compute_group = function(data, scales, width = .1, params) {
x <- data$x
y <- data$y
new_x <- c(rep(x + width, 2),
rep(x + 4 * width, 2),
rep(x + width, 2),
rep(x - width, 2),
rep(x - 4 * width, 2),
rep(x - width, 2))
new_y <- c(y + 4* width,
rep(y + width, 2),
rep(y - width, 2),
rep(y - 4*width, 2),
rep(y - width, 2),
rep(y + width, 2),
y + 4*width)
cross_data <- data.frame(x = new_x, y = new_y)
cross_data
},
required_aes = c("x", "y")
)
stat_cross <- function(mapping = NULL, data = NULL, geom = "polygon",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, width = 0.1, ...) {
layer(
stat = StatCross, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, width = width, ...)
)
}
iris %>%
mutate(index = row_number()) %>%
ggplot(aes(Sepal.Length, Sepal.Width, group = index)) +
stat_cross(width = .01, fill = "yellow", color = "black")

Created on 2021-03-15 by the reprex package (v1.0.0)