Skip to content

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

Merged
teunbrand merged 8 commits into
tidyverse:mainfrom
teunbrand:annotation_scale_transform
Nov 13, 2024
Merged

teunbrand merged 8 commits into
tidyverse:mainfrom
teunbrand:annotation_scale_transform

Conversation

@teunbrand

@teunbrand teunbrand commented Nov 12, 2024 •

Copy link
Copy Markdown
Collaborator

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

teunbrand commented Nov 12, 2024 •

Copy link
Copy Markdown
Collaborator Author

@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 :)

@yutannihilation yutannihilation left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Collaborator Author

Of course. Thank you for the review!

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