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

annotation_raster()/_custom() respond to scale transformations #6182

Merged
merged 8 commits into from
Nov 13, 2024

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Nov 12, 2024

This PR aims to fix #3120 and is incompatible with #3121.

Briefly, it uses the panel parameters to transform the corners of the rectangle used for annotation placement.

To recap some discussion in #3121. The scales should affect the annotation, but not vice versa (i.e., we don't want to train scales on annotations) #3121 (comment). The approach of encoding the corners coordinates as aesthetics did affect the scales, so that approach was unsuitable. Instead it was proposed to keep them as parameters and transform them in the drawing stage #3121 (comment). In addition, it was proposed to have data agnostic placement as well #3121 (comment). #3121 was waiting for #3116 to get implemented, but as this hasn't happened in 5 years, I felt it was time to forge ahead.

This PR implements transformed parameters, so scales are not trained by annotations. In addition, the I() mechanism (#5477) is used for data agnostic placement of annotations.

Some examples; plain annotation:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
rainbow <- matrix(pal_hue()(100), nrow = 1)
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  annotation_raster(rainbow, 15, 20, 3, 4)
p

Reversed x:

p + scale_x_reverse()

AsIs coordinates:

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  annotation_raster(rainbow, I(0.3), I(0.7), I(0.1), I(0.9))

Created on 2024-11-12 with reprex v2.1.1

@teunbrand
Copy link
Collaborator Author

teunbrand commented Nov 12, 2024

@yutannihilation Since you worked on this problem before (and I'm cannibalising your PR) in #3121, is there any chance I could get you to look this over at some point? No worries if it is inconvenient :)

Copy link
Member

@yutannihilation yutannihilation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this! I don't remember well how I struggled with #3121, but this just looks good to me.

AsIs coordinates:

This is cool feature and worth mentioning in NEWS.md. Could you add some words?

@teunbrand
Copy link
Collaborator Author

Of course. Thank you for the review!

@teunbrand teunbrand merged commit f468053 into tidyverse:main Nov 13, 2024
13 checks passed
@teunbrand teunbrand deleted the annotation_scale_transform branch November 13, 2024 08:24
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

Successfully merging this pull request may close these issues.

Objects added by annotation_custom not shown after scale_*_reverse
2 participants