Skip to content

WIP: make nudge_x and nudge_y aesthetics of geom_text() #3030

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

Closed
wants to merge 1 commit into from

Conversation

clauswilke
Copy link
Member

This is an example implementation of the feature I proposed in #3026 (comment). Not sure whether this is the right way to go or not.

library(ggplot2)

df <- data.frame(
  x = c(1,3,2,5),
  y = 1:4,
  label = c("a","c","d","c")
)

# new implementation, using parameters/aesthetics
p1 <- ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = label), nudge_y = c(-0.1, 0.1, -.5, -0.1))

# current implementation, using position adjustment
p2 <- ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = label), position = position_nudge(y = c(-0.1, 0.1, -.5, -0.1)))

# results are identical
cowplot::plot_grid(p1, p2)

cowplot::plot_grid(p1 + scale_y_log10(), p2 + scale_y_log10())

cowplot::plot_grid(p1 + coord_polar(), p2 + coord_polar())

cowplot::plot_grid(p1 + coord_trans(y = "sqrt"), p2 + coord_trans(y = "sqrt"))

# providing nudge values as aesthetics works
df <- data.frame(
  x = c(1,3,2,5),
  y = 1:4,
  nudge_y = c(-0.1, 0.1, -.5, -0.1),
  label = c("a","c","d","c")
)
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = label, nudge_y = nudge_y))

# incorrect length causes error as expected
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = label), nudge_y = c(-0.1, 0.1, -.5))
#> Error: Aesthetics must be either length 1 or the same as the data (4): nudge_y

# can specify both aesthetic and position, both nudgings are applied
ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(
    aes(label = label),
    nudge_y = c(-0.1, 0.1, -.5, -0.1),
    position = position_nudge(y = c(-0.1, 0.1, -.5, -0.1))
  )

Created on 2018-12-07 by the reprex package (v0.2.1)

@clauswilke
Copy link
Member Author

As discussed in #3026, this is probably not the right way to go. Instead, position adjustments should understand aesthetics. I'll close this PR.

@clauswilke clauswilke closed this Jan 20, 2019
@lock
Copy link

lock bot commented Jul 19, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jul 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant