Skip to content

Transformed axes do not respect reversing transforms #3133

Closed
@brianwdavis

Description

@brianwdavis

For a monotonic transformation that reverses the tonicity of the numbers (low-high -> high-low), secondary axes aren't rendering correctly.

You can see this with a reciprocal transform. The left-most break on top should be close to 1/min(mtcars$wt) or ~0.65. The right-most break should be close to 1/max(mtcars$wt) or ~0.20. For the right-hand transformed axis, the bottom-most break should be close to 1/0.20 or 5 (not 2), and the top-most break should be close to 1/0.65 or ~1.5 (not 5).

Note that these swaps have reversed the intended direction, but still aren't a 1:1 swap of positions.

This is even more obvious with a simple negative transform. Here on the y-axes, -5 maps to (closely, but not exactly) +2, and -2 maps to (closely, but not exactly) +5. And on the x-axes, +2 maps to ~-5. and +5 maps to ~-2.

This issue was noticed in a temperature transformation on a StackOverflow question:
https://stackoverflow.com/questions/54610847/ggplot2-secondary-axis-not-mapping-to-transformation

In both these figures, the values on the left-top axes and bottom-right axes should mirror each other around the diagonal.


library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.2

ggplot(mtcars, aes(wt, 1/wt)) +
  geom_point() +
  scale_x_continuous(
    sec.axis = sec_axis(trans = ~1/., breaks = seq(0, 1, by = 0.05))
  ) +
  scale_y_continuous(
    breaks = seq(0, 1, by = 0.05),
    sec.axis = sec_axis(trans = ~1/.)
  )

ggplot(mtcars, aes(wt, -wt)) +
  geom_point() +
  scale_x_continuous(
    sec.axis = sec_axis(trans = ~-.)
  ) +
  scale_y_continuous(
    sec.axis = sec_axis(trans = ~-.)
  )

Session info
sessionInfo()
#> R version 3.5.0 (2018-04-23)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 16299)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.1.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_0.12.17     xml2_1.2.0       bindr_0.1.1      knitr_1.20      
#>  [5] magrittr_1.5     tidyselect_0.2.4 munsell_0.4.3    colorspace_1.3-2
#>  [9] R6_2.2.2         rlang_0.3.1      httr_1.3.1       stringr_1.3.1   
#> [13] plyr_1.8.4       dplyr_0.7.5      tools_3.5.0      grid_3.5.0      
#> [17] gtable_0.2.0     withr_2.1.2      htmltools_0.3.6  yaml_2.1.19     
#> [21] lazyeval_0.2.1   rprojroot_1.3-2  digest_0.6.15    assertthat_0.2.0
#> [25] tibble_1.4.2     bindrcpp_0.2.2   purrr_0.2.4      curl_3.2        
#> [29] mime_0.5         glue_1.2.0       evaluate_0.10.1  rmarkdown_1.9   
#> [33] labeling_0.3     stringi_1.1.7    compiler_3.5.0   pillar_1.2.3    
#> [37] scales_0.5.0     backports_1.1.2  pkgconfig_2.0.1

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