Skip to content
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

Inf does not maintain annotation within boundaries of plot anymore #6111

Closed
rempsyc opened this issue Sep 17, 2024 · 4 comments
Closed

Inf does not maintain annotation within boundaries of plot anymore #6111

rempsyc opened this issue Sep 17, 2024 · 4 comments

Comments

@rempsyc
Copy link

rempsyc commented Sep 17, 2024

When adding an annotation to a plot, Inf used to keep the annotation within the frame of the plot. Recently however I noticed that this behaviour has changed with a recent update (of, probably, ggplot2 or one of its dependencies), such that the text is now almost completely hidden outside the borders of the frame when using this historically common "trick". I noticed this change of behaviour in my pkdown website and personal documents (old vs new).

Reprex of the new behaviour:

packageVersion("ggplot2")
#> [1] '3.5.1'
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
    annotate("text", label = "text", size = 15, x = Inf, y = Inf)

Created on 2024-09-17 with reprex v2.1.1

Same with I(1):

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
    annotate("text", label = "text", size = 15, x = I(1), y = I(1))

Created on 2024-09-17 with reprex v2.1.1

vjust and hjust used not to be necessary for this, or putting them at zero would put them at the edge, but now 0 completly hides:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
    annotate("text", label = "text", size = 15, x = Inf, y = Inf,
             vjust = 0, hjust = 0)

Created on 2024-09-17 with reprex v2.1.1

Whereas the old behaviour can be recovered using vjust and hjust of 1:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
    annotate("text", label = "text", size = 15, x = Inf, y = Inf,
             vjust = 1, hjust = 1)

Created on 2024-09-17 with reprex v2.1.1

Or with "inward":

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
    annotate("text", label = "text", size = 15, x = Inf, y = Inf,
             vjust = "inward", hjust = "inward")

Created on 2024-09-17 with reprex v2.1.1

Is this a bug or feature introduced by some new changes to ggplot2?

@teunbrand
Copy link
Collaborator

teunbrand commented Sep 17, 2024

Thanks for the report!
I don't recall the behaviour of automatically adjusting for proximity to the panel boundaries.
AFAIK you would've always needed to use hjust/vjust to get the positioning right.
When I use 3.4.3, I get the same thing as in 3.5.1, so it can't have been that recent of a change:

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.4.3'

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  annotate("text", label = "text", size = 15, x = Inf, y = Inf)

Created on 2024-09-17 with reprex v2.1.1

@rempsyc
Copy link
Author

rempsyc commented Sep 17, 2024

Thanks. While I have not changed my function rempsyc::nice_density in over a year, my previous rmarkdown analysis report shows this (as I remember my package website do as well in the examples):

image

But when I run the script again after a few weeks (and updating my packages), I now get:

image

That function relies on ggrepel, so... perhaps it was a change there? I tried testing on ggrepel first and got the same thing and so tried to see if I could nail it down further down the line to ggplot2, thus opening this issue. But perhaps ggrepel was adding automatic hjust and vjust and stopped doing that?

In any case, if that was always the behaviour, I think we can close the issue? Although I would have imagined that it would be sensible to have default values within the boundaries of the plot without having to make custom adjustments, but perhaps this is not easily generalizable to all the possible cases.

@smouksassi
Copy link

ggrepel geom_text_repel is not the same as geom_text indeed it used to put the inf into the panel a recent bug broke that but I think it was fixed in the last release:
slowkow/ggrepel#252

@teunbrand
Copy link
Collaborator

If this issue occurs in {ggrepel}, this is probably best fixed over there and we can close this issue here.
As a side-note, if you don't need to repel any points, it surprises me that you'd opt for a repelling text layer.

@rempsyc rempsyc closed this as completed Sep 17, 2024
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

No branches or pull requests

3 participants