Skip to content

Support for using I() to use NPC corodinates seems incomplete #5656

Closed

Description

@aphalo

@teunbrand I could not find an open issue on this, but I remember seeing a rather recent commit by you implementing this new feature. In 3.4.4.9000 I see it working in annotate() and in aes() but not with values mapped to constants in calls to geoms (at least geom_label()). By the way, it is great to see this implemented in 'ggplot2'. It makes several geometries in 'ggpp' redundant which is, I think, an indication of usefulness, as now all geometries become usable with NPC coordinates.

library(ggplot2)

ggplot(data = mtcars,
       mapping = aes(x = disp, y = mpg, colour = factor(cyl))) +
  geom_point() +
  annotate(geom = "label", x = I(0.5), y = I(0.9), label = "a label")

ggplot() +
  geom_point(mapping = aes(x = disp, y = mpg, colour = factor(cyl)),
             data = mtcars) +
  geom_label(mapping = aes(x = I(x), y = I(y), label = label),
             data = data.frame(x = 0.5, y = 0.9, label = "a label"))

ggplot(data = mtcars,
       mapping = aes(x = disp, y = mpg, colour = factor(cyl))) +
  geom_point() +
  geom_label(x = I(0.5), y = I(0.9), label = "a label", colour = "black")

ggplot(data = mtcars,
       mapping = aes(x = disp, y = mpg, colour = factor(cyl))) +
  geom_point() +
  geom_label(x = 300, y = 32, label = "a label", colour = "black")

Created on 2024-01-22 with reprex v2.1.0

Activity

  1. teunbrand commented on Jan 22, 2024

    @teunbrand
    Collaborator

    Thanks for the report @aphalo! I'm glad it come before the release rather than afterwards. I agree with you about the usefulness of non-data coordinates.
    The culprit of this behaviour seems to be here:

    data[aes_params] <- params[aes_params]

    Where the AsIs class seem to be dropped. You can replicate that behaviour like this. Note that the foo column is not an AsIs column anymore.

    df <- mtcars
    df["foo"] <- list(foo = I(10))
    class(df$foo)
    #> [1] "numeric"

    Created on 2024-01-22 with reprex v2.1.0

    One possible way to remediate this, would be to convert to a list, do the assignment, and restore to data.frame again.

    df <- unclass(mtcars)
    df["foo"] <- list(foo = I(10))
    df <- vctrs::data_frame(!!!df)
    class(df$foo)
    #> [1] "AsIs"

    Created on 2024-01-22 with reprex v2.1.0

  2. teunbrand commented on Jan 23, 2024

    @teunbrand
    Collaborator

    Considered fixed in the RC by #5657

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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