Skip to content

Dodge points vertically #5845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `geom_point()` can be dodged vertically by using
`position_dodge(..., orientation = "y")` (@teunbrand, #5809).
* Fixed bug where `na.value` was incorrectly mapped to non-`NA` values
(@teunbrand, #5756).
* Fixed bug in `guide_custom()` that would throw error with `theme_void()`
Expand Down
11 changes: 8 additions & 3 deletions R/position-dodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#' geoms. See the examples.
#' @param preserve Should dodging preserve the `"total"` width of all elements
#' at a position, or the width of a `"single"` element?
#' @param orientation Fallback orientation when the layer or the data does not
#' indicate an explicit orientation, like `geom_point()`. Can be `"x"`
#' (default) or `"y"`.
#' @family position adjustments
#' @export
#' @examples
Expand Down Expand Up @@ -79,10 +82,11 @@
#'
#' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
#' geom_bar(position = position_dodge2(preserve = "total"))
position_dodge <- function(width = NULL, preserve = "total") {
position_dodge <- function(width = NULL, preserve = "total", orientation = "x") {
ggproto(NULL, PositionDodge,
width = width,
preserve = arg_match0(preserve, c("total", "single"))
preserve = arg_match0(preserve, c("total", "single")),
orientation = arg_match0(orientation, c("x", "y"))
)
}

Expand All @@ -93,8 +97,9 @@ position_dodge <- function(width = NULL, preserve = "total") {
PositionDodge <- ggproto("PositionDodge", Position,
width = NULL,
preserve = "total",
orientation = "x",
setup_params = function(self, data) {
flipped_aes <- has_flipped_aes(data)
flipped_aes <- has_flipped_aes(data, default = self$orientation == "y")
data <- flip_data(data, flipped_aes)
if (is.null(data$xmin) && is.null(data$xmax) && is.null(self$width)) {
cli::cli_warn(c(
Expand Down
7 changes: 4 additions & 3 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ switch_orientation <- function(aesthetics) {
#' @param main_is_optional Is the main axis aesthetic optional and, if not
#' given, set to `0`
#' @param flip Logical. Is the layer flipped.
#' @param default The logical value to return if no orientation can be discerned
#' from the data.
#'
#' @return `has_flipped_aes()` returns `TRUE` if it detects a layer in the other
#' orientation and `FALSE` otherwise. `flip_data()` will return the input
Expand All @@ -492,7 +494,7 @@ switch_orientation <- function(aesthetics) {
has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
range_is_orthogonal = NA, group_has_equal = FALSE,
ambiguous = FALSE, main_is_continuous = FALSE,
main_is_optional = FALSE) {
main_is_optional = FALSE, default = FALSE) {
# Is orientation already encoded in data?
if (!is.null(data$flipped_aes)) {
not_na <- which(!is.na(data$flipped_aes))
Expand Down Expand Up @@ -561,8 +563,7 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
}
}

# default to no
FALSE
isTRUE(default)
}
#' @rdname bidirection
#' @export
Expand Down
6 changes: 5 additions & 1 deletion man/bidirection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/position_dodge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-position_dodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ test_that("can control whether to preserve total or individual width", {
expect_equal(layer_data(p_total)$x, new_mapped_discrete(c(1, 1.75, 2.25)))
expect_equal(layer_data(p_single)$x, new_mapped_discrete(c(0.75, 1.75, 2.25)))
})

test_that("position_dodge() can dodge points vertically", {

df <- data.frame(x = c(1, 2, 3, 4), y = c("a", "a", "b", "b"))

horizontal <- ggplot(df, aes(y, x, group = seq_along(x))) +
geom_point(position = position_dodge(width = 1, orientation = "x"))
vertical <- ggplot(df, aes(x, y, group = seq_along(x))) +
geom_point(position = position_dodge(width = 1, orientation = "y"))

expect_equal(layer_data(horizontal)$x, c(0.75, 1.25, 1.75, 2.25), ignore_attr = "class")
expect_equal(layer_data(vertical)$y, c(0.75, 1.25, 1.75, 2.25), ignore_attr = "class")

})
Loading