Skip to content

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

Closed
@rempsyc

Description

@rempsyc

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions