-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Could position nudging be done in an easy proportional way relative to scale as a whole (e.g. using I function)? #6466
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
Comments
you can nudge by a fixed amount or proportional to your x value library(tidyverse)
library(tidyplots)
library(patchwork)
#currrently do this
a<- spendings |>
group_by(category) |>
summarise(across(amount, sum)) |>
ggplot(aes(x = amount, y = category, label = amount)) +
geom_col() +
geom_text(aes(x=amount*1.2)) +
labs(subtitle = "nudge 20%")
b<- spendings |>
group_by(category) |>
summarise(across(amount, sum)) |>
ggplot(aes(x = amount, y = category, label = amount)) +
geom_col() +
geom_text(aes(x=amount+50)) +
labs(subtitle = "nudge +50 x units")
a | b Created on 2025-05-20 with reprex v2.1.1 |
Interesting, thanks @smouksassi I want to be able to easily make the nudging the same for each value, and proportional to the whole of a scale. So multiplying by the aesthetic doesn't work, as large values then get larger nudging. Adding works but it's not proportional without doing some calcs |
In the development version, since |
That looks pretty cool @teunbrand However, it would still make the nudge proportional to the max value, rather than proportional to the entire scale size - which is what I want because I often change the limits, expand etc. This code nudges relative to the max value, as I understand it. I'd say the use of after_stat is still pretty challenging for non-advanced ggplot2 users too. Is the I solution possible? This would align nicely with how this works in annotating ![]() |
Ideally, I want to be able to add a line of code like this, which would add some space equivalent to that proportion of the scale as a whole...
Not sure if it'd be possible.. library(tidyverse)
library(tidyplots)
# can do this based on the max value for where scale min is 0
spendings |>
group_by(category) |>
summarise(across(amount, sum)) |>
ggplot(aes(x = amount, y = category, label = amount)) +
geom_col() +
geom_text(aes(x = amount + max(amount) * 0.01), hjust = 0) +
scale_x_continuous(expand = expansion(c(0, 0.1))) # #prefer to do this based on the scale size, which would work for any limits/expand combo consistently
# spendings |>
# group_by(category) |>
# summarise(across(amount, sum)) |>
# ggplot(aes(x = amount, y = category, label = amount)) +
# geom_col() +
# geom_text(aes(x = amount + I(0.02)), hjust = 0) +
# scale_x_continuous(expand = expansion(c(0, 0.1))) Created on 2025-05-22 with reprex v2.1.1 |
That sounds like you want the same as #5609, in which case, I'll mark this as a duplicate to prune discussion here so it may grow there. |
It'd be nice to be able to nudge in a proportional way easily. At present, you can calculate your nudge manually in absolute units, but it'd nice if there was a more simple straightforward way.
The
I
function allows relative positioning when annotating. So was wondering if it'd be possible to support this with nudging? This is addingnudge_x =I(0.05)
orposition_nudge(x = I(0.05))
to do this, rather than having to manually calculate.Sorry for all the issues!
Created on 2025-05-21 with reprex v2.1.1
The text was updated successfully, but these errors were encountered: