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

Discrete secondary axes #5620

Merged
merged 12 commits into from
May 20, 2024
Merged

Discrete secondary axes #5620

merged 12 commits into from
May 20, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #3171.

Briefly, it adds a sec.axis option for scale_{x/y}_discrete().

For context:
The secondary axis system is not ideal. We have tried to move that functionality to guides in #5410, but it becomes too complicated. Because it doesn't seem viable to handle this functionality in guides, I think it is time to implement secondary axes for discrete scales.

In some more detail:
It currently only accepts identity transformations, as it just doesn't make sense to me why one would transform a discrete axis.
This also means that dup_axis() should be preferred to sec_axis() for discrete scales.
Breaks can be given as numeric that can take arbitrary positions, or as discrete breaks that can take one of the limits values.
For discrete scales, breaks are mapped early on in AxisSecondary$break_info().

An example plot with relabelling the x-axis and numeric positions in the y-axis:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

data <- data.frame(x = c("A", "B", "C"), y = c("D", "E", "F"))
ggplot(data, aes(x, y)) +
  geom_point() +
  scale_x_discrete(sec.axis = dup_axis(
    labels = c("foo", "bar", "baz"),
    name = "Secondary x-axis"
  )) +
  scale_y_discrete(sec.axis = dup_axis(
    breaks = c(1.5, 2.5), labels = c("grault", "garply"),
    name = "Secondary y-axis"
  ))

Created on 2024-01-02 with reprex v2.0.2

@@ -130,6 +130,11 @@ is.sec_axis <- function(x) {

set_sec_axis <- function(sec.axis, scale) {
if (!is.waive(sec.axis)) {
if (scale$is_discrete()) {
if (!identical(.subset2(sec.axis, "transform"), identity)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .subset2() prevents wrapping the function into a ggproto method.

@llrs
Copy link
Contributor

llrs commented Jan 29, 2024

Hi, many thanks for implementing this!
I see in preparation the next ggplot2 release, is it going to make it in time?

@teunbrand
Copy link
Collaborator Author

I'm afraid this will not be included in the upcoming release.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please add news bullet

Merge branch 'main' into discrete_sec_axis

# Conflicts:
#	R/axis-secondary.R
#	man/sec_axis.Rd
@teunbrand teunbrand merged commit 7f6ca67 into tidyverse:main May 20, 2024
11 checks passed
@teunbrand teunbrand deleted the discrete_sec_axis branch May 20, 2024 07:34
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.

support sec.axis for scale_discrete
3 participants