From b242644fd58b75c0d5c40a71ccd4775fd0de07b6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 30 Sep 2025 08:58:36 +0200 Subject: [PATCH 1/5] add `group_row` parameter --- R/position-dodge2.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/position-dodge2.R b/R/position-dodge2.R index 168bc6c287..e85786f527 100644 --- a/R/position-dodge2.R +++ b/R/position-dodge2.R @@ -3,12 +3,14 @@ #' @param padding Padding between elements at the same position. Elements are #' shrunk by this proportion to allow space between them. Defaults to 0.1. position_dodge2 <- function(width = NULL, preserve = "total", - padding = 0.1, reverse = FALSE) { + padding = 0.1, reverse = FALSE, + group_row = "single") { ggproto(NULL, PositionDodge2, width = width, preserve = arg_match0(preserve, c("total", "single")), padding = padding, - reverse = reverse + reverse = reverse, + group_row = group_row ) } @@ -20,6 +22,7 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge, preserve = "total", padding = 0.1, reverse = FALSE, + group_row = "single", setup_params = function(self, data) { flipped_aes <- has_flipped_aes(data) @@ -48,7 +51,8 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge, n = n, padding = self$padding, reverse = self$reverse, - flipped_aes = flipped_aes + flipped_aes = flipped_aes, + group_row = self$group_row ) }, From 3e667f9e4c71f5d8ba748cfc4aec7a183a895584 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 30 Sep 2025 09:43:53 +0200 Subject: [PATCH 2/5] run-length encode data for violins --- R/position-dodge2.R | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/R/position-dodge2.R b/R/position-dodge2.R index e85786f527..3260fd3a92 100644 --- a/R/position-dodge2.R +++ b/R/position-dodge2.R @@ -58,8 +58,15 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge, compute_panel = function(data, params, scales) { data <- flip_data(data, params$flipped_aes) + key <- NULL + columns <- c("group", "x", "xmin", "xmax") + if (isTRUE(params$group_row == "many")) { + # Run-length encode (RLE) relevant variables + key <- vec_unrep(data[columns]) + } + collided <- collide2( - data, + key$key %||% data, params$width, name = "position_dodge2", strategy = pos_dodge2, @@ -68,7 +75,15 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge, check.width = FALSE, reverse = params$reverse ) - flip_data(collided, params$flipped_aes) + + if (!is.null(key)) { + # Decode RLE to full data + data[columns] <- vec_rep_each(collided[columns], key$times) + } else { + data <- collided + } + + flip_data(data, params$flipped_aes) } ) From e4da4a604ea695cba732faaf2b8011fd3371fae9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 30 Sep 2025 09:44:17 +0200 Subject: [PATCH 3/5] document --- R/position-dodge2.R | 5 ++++- man/position_dodge.Rd | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/R/position-dodge2.R b/R/position-dodge2.R index 3260fd3a92..b452557889 100644 --- a/R/position-dodge2.R +++ b/R/position-dodge2.R @@ -2,6 +2,9 @@ #' @rdname position_dodge #' @param padding Padding between elements at the same position. Elements are #' shrunk by this proportion to allow space between them. Defaults to 0.1. +#' @param group_row Relationship between groups and rows. Can be `"single"` if +#' every row represents a single group, or `"many"` if many rows represent +#' a group. position_dodge2 <- function(width = NULL, preserve = "total", padding = 0.1, reverse = FALSE, group_row = "single") { @@ -10,7 +13,7 @@ position_dodge2 <- function(width = NULL, preserve = "total", preserve = arg_match0(preserve, c("total", "single")), padding = padding, reverse = reverse, - group_row = group_row + group_row = arg_match0(group_row, c("single", "many")) ) } diff --git a/man/position_dodge.Rd b/man/position_dodge.Rd index cd8b3adbe7..21f4e1cb0a 100644 --- a/man/position_dodge.Rd +++ b/man/position_dodge.Rd @@ -16,7 +16,8 @@ position_dodge2( width = NULL, preserve = "total", padding = 0.1, - reverse = FALSE + reverse = FALSE, + group_row = "single" ) } \arguments{ @@ -36,6 +37,10 @@ This is useful if you're rotating both the plot and legend.} \item{padding}{Padding between elements at the same position. Elements are shrunk by this proportion to allow space between them. Defaults to 0.1.} + +\item{group_row}{Relationship between groups and rows. Can be \code{"single"} if +every row represents a single group, or \code{"many"} if many rows represent +a group.} } \description{ Dodging preserves the vertical position of an geom while adjusting the From 85e8f262fe53dff7c6dbaaf6733de4896eb42136 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 30 Sep 2025 09:52:17 +0200 Subject: [PATCH 4/5] use dot.case for argument name --- R/position-dodge2.R | 4 ++-- man/position_dodge.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/position-dodge2.R b/R/position-dodge2.R index b452557889..31b3fd507a 100644 --- a/R/position-dodge2.R +++ b/R/position-dodge2.R @@ -7,13 +7,13 @@ #' a group. position_dodge2 <- function(width = NULL, preserve = "total", padding = 0.1, reverse = FALSE, - group_row = "single") { + group.row = "single") { ggproto(NULL, PositionDodge2, width = width, preserve = arg_match0(preserve, c("total", "single")), padding = padding, reverse = reverse, - group_row = arg_match0(group_row, c("single", "many")) + group_row = arg_match0(group.row, c("single", "many")) ) } diff --git a/man/position_dodge.Rd b/man/position_dodge.Rd index 21f4e1cb0a..6051c7bd13 100644 --- a/man/position_dodge.Rd +++ b/man/position_dodge.Rd @@ -17,7 +17,7 @@ position_dodge2( preserve = "total", padding = 0.1, reverse = FALSE, - group_row = "single" + group.row = "single" ) } \arguments{ From 9b61ecb298a427e0f24855518923ac873f708f53 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 30 Sep 2025 10:11:28 +0200 Subject: [PATCH 5/5] lol at my incompetence --- R/position-dodge2.R | 2 +- man/position_dodge.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/position-dodge2.R b/R/position-dodge2.R index 31b3fd507a..b6f70a952d 100644 --- a/R/position-dodge2.R +++ b/R/position-dodge2.R @@ -2,7 +2,7 @@ #' @rdname position_dodge #' @param padding Padding between elements at the same position. Elements are #' shrunk by this proportion to allow space between them. Defaults to 0.1. -#' @param group_row Relationship between groups and rows. Can be `"single"` if +#' @param group.row Relationship between groups and rows. Can be `"single"` if #' every row represents a single group, or `"many"` if many rows represent #' a group. position_dodge2 <- function(width = NULL, preserve = "total", diff --git a/man/position_dodge.Rd b/man/position_dodge.Rd index 6051c7bd13..ebe241d1d7 100644 --- a/man/position_dodge.Rd +++ b/man/position_dodge.Rd @@ -38,7 +38,7 @@ This is useful if you're rotating both the plot and legend.} \item{padding}{Padding between elements at the same position. Elements are shrunk by this proportion to allow space between them. Defaults to 0.1.} -\item{group_row}{Relationship between groups and rows. Can be \code{"single"} if +\item{group.row}{Relationship between groups and rows. Can be \code{"single"} if every row represents a single group, or \code{"many"} if many rows represent a group.} }