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

Data independent/relative scales for annotation_custom() #5417

Closed
half-normal opened this issue Sep 12, 2023 · 7 comments
Closed

Data independent/relative scales for annotation_custom() #5417

half-normal opened this issue Sep 12, 2023 · 7 comments
Labels
feature a feature request or enhancement

Comments

@half-normal
Copy link

Perhaps there is already a way to do this, but can we somehow get data independent scales for the annotation_custom() function.

I will often want to produce the same plot for different response variables and and want an icon in the top left corner. I know I could do this by getting the max of the y variable and setting ymin/ymax relative to that. However, it would be nice to just have a 0-1 coordinate system that sits over any plot, whether it be a single plot, combination of plots, facet wrap plots, etc. Then annotations can be added at the end in a consistent way.

@half-normal
Copy link
Author

@smouksassi As far as I can tell the first only works for text labels, the second works nicely, but not for faceted plots (puts the annotation in every facet). What I am after is a simple way of putting PNG icons/pictures on a plot in a way which is independent of the plot itself (i.e. does not depend on what is in the plot such as axis scales, facets, etc).

@smouksassi
Copy link

Ok care to share some code or mock end results you are after?

@teunbrand
Copy link
Collaborator

See also #5290 for proposed mechanism of scale-independent aesthetics.

@half-normal
Copy link
Author

half-normal commented Sep 13, 2023

@smouksassi here is a function I wrote which does something like what I am after. A couple limitations are that it can't be added (with +) to the plot like a regular ggplot layer and it only plots within the x and y axis bounds. I will probably look into how to change the latter when I get some time.

annotate_grob <- function(plot, grob, xmin, xmax, ymin, ymax, data_scale = FALSE){
  library(ggplot2)
  if (!data_scale) {
    # Min and max values from plot axes
    min_x <- layer_scales(plot)$x$range$range[1]
    max_x <- layer_scales(plot)$x$range$range[2]
    min_y <- layer_scales(plot)$y$range$range[1]
    max_y <- layer_scales(plot)$y$range$range[2]
    x_len <- max_x - min_x
    y_len <- max_y - min_y
    # Create grob boundaries as scaled versions of min/max values
    xmin <- min_x + x_len*xmin
    xmax <- min_x + x_len*xmax
    ymin <- min_y + y_len*ymin
    ymax <- min_y + y_len*ymax
  }
  plot + annotation_custom(
    grob, xmin, xmax, ymin, ymax
  )
}

@aphalo
Copy link
Contributor

aphalo commented Sep 14, 2023

You may want to try geom_text_npc() or geom_label_npc() from package 'ggpp'. They are geometries, so they work with facets, even with free scales. They use "pseudo aesthetics" npcx and npcy, and internally do computaions similar to those in your mock function. I have included also other geoms based on the same mechanism, even for plots, tables and grobs. 'ggpp' is in CRAN and the documentation is on-line, the documentation contains quite a few examples. These geoms can also be used with 'annotate()' (slightly modified in 'ggpp' so that it accepts and passes npcx and npcy).

@teunbrand teunbrand added the feature a feature request or enhancement label Oct 13, 2023
@teunbrand
Copy link
Collaborator

I consider this issue fixed by #5477.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants