Better secondary axis tick positioning #3581
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR suggests a solution that addresses issue #3576.
When plotting the secondary axis, tick positions of the secondary should be transformed to the range of the primary axis (
AxisSecondary$break_info()
). To achieve this, a grid (old_range
) is generated (of the sizeAxisSecondary$detail
, defaulted to1000
). This grid is then transformed to the secondary axis scale (namedfull_range
). Each tick of the secondary axis is transformed to the primary's scale by finding theindex
of the closest value in thefull_range
(index <- which.min(abs(full_range - x))
) and adoptingold_range[index]
as tick position in the primary axis scale.It is easy to understand that if
full_range
does not contain values equal to the secondary axis ticks, transformation is not accurate and the quality of this transformation depends on the grid size. This is observed in #3576, when duplicated axis (dup_axis
) cannot properly position its ticks.A simple alternative solution is to introduce linear interpolation. Instead of using the closest grid value, it is better to use two grid values and interpolate in between to increase the quality of the transformation. This works exceptionally well in the case of linear transformations of the secondary axis, including duplicated axes (
dup_axis
).The method works well for different combinations of primary/secondary transformations, including when one axis is monotonically increasing while another is decreasing. There are no checks for boundary cases, i.e. when ticks are found at the edges of the grid, which I assume never happens.