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

Enhance action "spread" in position_nudge_to() #6

Open
aphalo opened this issue Oct 8, 2022 · 4 comments
Open

Enhance action "spread" in position_nudge_to() #6

aphalo opened this issue Oct 8, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@aphalo
Copy link
Owner

aphalo commented Oct 8, 2022

This is a note about something to consider in the future. In 'ggrepel' repulsion is applied during rendering, so it tracks changes in plot size. This is advantageous in some respects, but means that repulsion will result in different plot layouts depending on the graphical device and width and height parameters passed to it. A different compromise solution is to apply the repulsion with a position function.

While position_repulse() would depend on a random shift component, position_spread() would work deterministically.

@aphalo aphalo added the enhancement New feature or request label Oct 8, 2022
@aphalo aphalo self-assigned this Oct 8, 2022
@aphalo aphalo changed the title Repulsive and spreading position functions position_repulse() and position_spread(): two position functions for the future Oct 8, 2022
@aphalo aphalo changed the title position_repulse() and position_spread(): two position functions for the future position_repulse() and position_spread() as two position functions for the future Oct 8, 2022
@aphalo aphalo added the on hold To be reconsidered at a later time label Feb 17, 2023
@aphalo aphalo changed the title position_repulse() and position_spread() as two position functions for the future Action "repulse" in position_nudge_to() Mar 3, 2023
@aphalo
Copy link
Owner Author

aphalo commented Mar 3, 2023

No new position functions are needed for this, so I changed the title. Action "spread" is now implemented. I think repulsion is overkill in this case. Adding support for spreading to a minimum distance would be deterministic and easier to implement.

@aphalo
Copy link
Owner Author

aphalo commented Dec 25, 2023

Action "spread" in position_nudge_to() within a range arranges the new positions to be equally spaced. An alternative approach to spreading would be to deterministically displace the positions just to ensure a minimum distance, but not more than this. Finding the optimal displacement will probably require iteration using a penalty. Possibly, the penalty could be a sum of squares to ensure that the larger displacements are penalized more than the sum of smaller ones. Someone must have developed an algorithm for this. Maybe asking in Mastodon would be a good idea.

@aphalo aphalo changed the title Action "repulse" in position_nudge_to() Enhance action "spread" in position_nudge_to() Dec 25, 2023
@aphalo
Copy link
Owner Author

aphalo commented Jun 24, 2024

Some investigation suggests that compute_panel instead of compute_layer should be used, as it takes scales as argument.
Currently action "spread" distributes the labels within the range of the data. An action "expand" could be implemented to use the scale range instead. To add even more flexibility, user-supplied functions could be accepted as arguments for action although the interface would need some thought to make it possible to use user-passed functions with both data and scale ranges. So, instead of implementing "expand", we could add x.range and y.range formal parameters, thus making it possible to also pass a numeric range, "data" or "scale" as arguments.

This needs quite a bit of thought as most likely user passed functions will need additional arguments. A different approach using more formal arguments would to have x.action.fun and y.action.fun and the matching x.action.fun.args and y.action.fun.args and x.range and y.range in a position function called position_nudge_fun() and implement position_nudge_to() and other similar position functions as wrappers on position_nudge_fun().

help(ggproto) and https://ggplot2-book.org/internals#sec-ggproto, and https://ggplot2-book.org/extensions#new-positions

@aphalo aphalo removed the on hold To be reconsidered at a later time label Jun 24, 2024
@aphalo
Copy link
Owner Author

aphalo commented Jun 25, 2024

Using compute_panel works only if all layers share the same data, at least without implementing a work-around. I ended implementing the ability to use an expanded or contracted range based on the data in the layer. Does not necesarily make use of all the available room for spreading the positions but gives an option that does not require arbitrary values entered mannually. Action "expand" is not needed. Parameter distance is ready to be used when a different strategy for spreading is implemented in the future. Current strategy is distance = "equal". In the future a different strategy that only ensures a minimum distance would be useful.

At this point, I set this issue back to on hold for the implementation of a different strategy. Alternatively, functions could be accepted as arguments to distance.

  • Implement action "spread". (Implemented in 2023-03)
  • Make it possible to expand and contract the range. (As implemented this works both with default and user supplied values for x and y.)
  • Add unit tests.
  • Implement an spreading strategy that displaces positions only enough to ensure a minimum separation or distance.
  • Add unit tests.
  • Support user-defined functions for arbitrary spreading strategies.
  • Add unit tests.
  • Test and tweak the code to ensure that spreading of positions works correctly when nudging is applied to both x and y.

@aphalo aphalo added the on hold To be reconsidered at a later time label Jun 25, 2024
@aphalo aphalo modified the milestones: Future version, v0.5.9 Jun 26, 2024
@aphalo aphalo removed on hold To be reconsidered at a later time labels Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant