Skip to content

Document vector passing to nudge_y/nudge_x/position_nudge #6335

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 4 commits into from
Feb 25, 2025

Conversation

MaxAtoms
Copy link
Contributor

Recently, when creating plots with ggstatsplot (using the ggbetweenstats function), I wanted to move the centrality labels for each distribution individually. It took me quite some time to figure out that I can pass a vector to nudge_y (or position_nudge for that matter).

Therefore, I propose to document this functionality.

Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! We generally don't want to encourage passing aesthetics outside aes() as vectors for the reasons documented in most layers like ?geom_point:

Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.

If you want to pass it like aes(nudge_y = c(...)) that'd be fine with me. If you intend to change the example, would you mind running devtools::document() so that the changes propagate to the .Rd files?

@MaxAtoms
Copy link
Contributor Author

MaxAtoms commented Feb 21, 2025

Sorry, if I am ignorant here, but wrapping nudge_y in aes() gives me Ignoring unknown aesthetics: nudge_y.
Am I understanding something wrong here?

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y, nudge_y = c(-0.1, 0.1, -0.1, 0.1)))

The same error happens when setting position = position_nudge withinaes().

@teunbrand
Copy link
Collaborator

teunbrand commented Feb 21, 2025

The development version of ggplot2 understands nudge_x and nudge_y to be aesthetics of position_nudge(), which isn't on CRAN yet. One of the motivating reasons was exactly because we don't want to encourage vectorised input to parameters/aesthetic outside the aes() function.

# Load the development version on my personal machine
devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

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

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y, nudge_y = c(-0.1, 0.1, -0.1, 0.1)))

Created on 2025-02-21 with reprex v2.1.1

@MaxAtoms
Copy link
Contributor Author

Ah, I see. So you might want to change the target branch of this PR to a development branch?

I added the change and ran devtools::document().
Please note that there also changes to hmisc.Rd.

Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me: thank you very much for the contribution!

you might want to change the target branch of this PR to a development branch

The main branch is the development branch, the released versions are tagged.

@teunbrand teunbrand merged commit d1fb550 into tidyverse:main Feb 25, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants