Skip to content

relative placement of annotations #1244

Closed

Description

@gdkrmr

Feature request:
It would be really useful if annotations could be placed like legends indicating their relative position (legend.placement = c(1,0)) in the plot area.

Coordinate limits can be extracted after building the plot:
https://gist.github.com/tomhopper/9076152

Being able to do this before building the plot would be a really neat feature, e.g. for placing regression formulae nicely in a corner.
Something like:

g + annotate("text", x = get_xlim()[2], y = get_ylim()[2], label = "y = 3 + 4x")

Activity

  1. hadley commented on Aug 5, 2015

    @hadley
    Member

    You can already do it :) (for that special case)

    library(ggplot2)
    ggplot(mpg, aes(displ, hwy)) + 
      geom_point() + 
      annotate("text", -Inf, Inf, label = "Top-left", hjust = 0, vjust = 1)
  2. gdkrmr commented on Aug 5, 2015

    @gdkrmr
    Author

    Thanks, I wasn't aware of that!

  3. gdkrmr commented on Aug 5, 2015

    @gdkrmr
    Author

    @hadley btw. this feature is missing in the documentation

  4. hadley commented on Aug 5, 2015

    @hadley
    Member

    It's in the book ;)

  5. baptiste commented on Aug 8, 2015

    @baptiste
    Contributor

    the trick also applies to annotation_custom; while answering it the thought occurred to me that one could define complex positions, such as x = 1 - 2i, which would mean "2 data units to the left of the right edge". (actually, I was thinking of absolute vs relative grid units, but it's a similar idea). Claudia B. inspired this with her hyperSpec package, where complex numbers are used to encode similarly dual information.

    Just throwing the idea out there, just in case :)

  6. kendonB commented on Nov 29, 2016

    @kendonB

    Making this explicit and separated by axis would be a welcome addition. For example, I'd like to be able to choose the x-value of an annotation explicitly and have the y-value 50% up the plot.

  7. baptiste commented on Nov 29, 2016

    @baptiste
    Contributor

    @kendonB qplot(1:10,1:10, geom="blank") + annotation_custom(textGrob("y centered"),xmin=3,xmax=3)

  8. kendonB commented on Nov 29, 2016

    @kendonB

    Thanks so much @baptiste. Is there any way to also have hjust/vjust in this?

  9. colinbrislawn commented on May 9, 2018

    @colinbrislawn

    It would be really useful if annotations could be placed like legends indicating their relative position (legend.placement = c(1,0)) in the plot area.

    I agree!

    annotate("text", -Inf, Inf, ...

    That solution will place a annotation into every facet, where legend.placement places a single annotation based on the relative position in the graph, regardless of the other geoms or facets being used.

    I would like to see something like annotate_relative or annotate_global to provide this functionality.

  10. baptiste commented on May 9, 2018

    @baptiste
    Contributor

    annotation_custom provides a way by wrapping an arbitrary grob in a gTree that spans the full panel. The syntax is not super convenient, especially since one has to load grid to use unit and gTree, but there could be a convenience function around this.

    library(ggplot2)
    library(grid)
    
    g <- grobTree(textGrob('here', x=unit(0.8, "npc"), y=unit(0.5,"npc")))
    qplot(0:10,0:10) +
      annotate("point",x=8,y=5,col="red") +
      annotation_custom(g)
    

    To specify specific facets, one would need a geom_custom, which exists as proof-of-principle in egg, but not on CRAN because ggplot2 internal changes in layout.

  11. lock commented on Nov 5, 2018

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  12. locked and limited conversation to collaborators on Nov 5, 2018
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