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

sec.axis with scale_*_log10 #1992

Closed
pnolain opened this Issue Jan 11, 2017 · 9 comments

Comments

Projects
None yet
6 participants
@pnolain

pnolain commented Jan 11, 2017

Hi,

I have been trying to plot continuous data with a log-scaled x-axis and a secondary x-axis in order to display ticks values in another unit.
It seems to me that the ticks positions of this secondary axis are not correctly computed.

Here is a reproducible example:

foo <- tibble(x = exp(seq(log(0.001), log(1000), length.out = 100)), y = x/(1+x))

ggplot(foo, aes(x, y))+
  geom_point()+
  scale_x_log10(name = "Unit A",
                breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),
                sec.axis = sec_axis(~. * 100, name = "Unit B", 
                                    labels = derive(),
                                    breaks = derive()))

sec_axis1
Considering the transformation (x * 100), ticks positions do not correspond.

Also, dup_axis() does not duplicate the original axis:

ggplot(foo, aes(x, y))+
  geom_point()+
  scale_x_log10(name = "Unit A",
                breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),
                sec.axis = dup_axis())

sec_axis2

Am I doing something wrong?

@hadley

This comment has been minimized.

Member

hadley commented Jan 25, 2017

@thomasp85 can you take a look?

@thomasp85 thomasp85 self-assigned this Feb 28, 2017

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Apr 3, 2017

Hmm... I know where the problem lies, but I'm unsure what the best approach is to fix it.

Basically, the way the secondary axis works without requiring the user to supply a reverse transform is that it calculates the position for 1000 points evenly spaced in the original range and choses the ones closest to the breaks. The log transform of the original axis makes the uniformly distributed points very very coarse on the left side resulting in the strange bug observed.

Increasing the number of points is unfeasible. Using 1e5 only fixes one tick and is already taxing on the render time. Thus, a whole new approach is needed...

I'll mull over this some more, but if someone hase some great insight I'll be happy to hear it

@hadley

This comment has been minimized.

Member

hadley commented Apr 3, 2017

Maybe we could optionally allow supplying the inverse?

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Apr 3, 2017

That would be the last resort, but I would prefer something more elegant. I'll need to find a way to sample point uniformly over the transformed range instead but all of this code is quite distant so I'll have to think a bit🙂

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Apr 3, 2017

Oh, but that is actually easy — I'll make a fix tomorrow

@amignan

This comment has been minimized.

amignan commented Dec 11, 2017

Hi, I'm observing the same bug. I updated to the latest version of ggplot2 at no avail. Is the fix published? Thanks

@jacobgrasmick

This comment has been minimized.

jacobgrasmick commented May 20, 2018

I also am observing this bug. Has it been fixed? I'm using version 2.2.1
Thanks!

@clauswilke

This comment has been minimized.

Member

clauswilke commented May 20, 2018

This seems to work as expected in the development version, which is currently being prepared for release as 2.3.0.

library(ggplot2)
library(tibble)

foo <- tibble(x = exp(seq(log(0.001), log(1000), length.out = 100)), y = x/(1+x))

ggplot(foo, aes(x, y))+
  geom_point()+
  scale_x_log10(name = "Unit A",
                breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),
                sec.axis = sec_axis(~. * 100, name = "Unit B", 
                                    labels = derive(),
                                    breaks = derive()))

ggplot(foo, aes(x, y))+
  geom_point()+
  scale_x_log10(name = "Unit A",
                breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000),
                sec.axis = dup_axis())

Created on 2018-05-20 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-05-20
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                          
#>  assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                  
#>  backports    1.1.2      2017-12-13 CRAN (R 3.5.0)                  
#>  base       * 3.5.0      2018-04-24 local                           
#>  bindr        0.1.1      2018-03-13 CRAN (R 3.5.0)                  
#>  bindrcpp     0.2        2017-06-17 CRAN (R 3.5.0)                  
#>  colorspace   1.4-0      2017-12-23 R-Forge (R 3.5.0)               
#>  compiler     3.5.0      2018-04-24 local                           
#>  curl         3.1        2017-12-12 CRAN (R 3.5.0)                  
#>  datasets   * 3.5.0      2018-04-24 local                           
#>  devtools     1.13.5     2018-02-18 CRAN (R 3.5.0)                  
#>  digest       0.6.15     2018-01-28 CRAN (R 3.5.0)                  
#>  dplyr        0.7.4      2017-09-28 CRAN (R 3.5.0)                  
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.5.0)                  
#>  ggplot2    * 2.2.1.9000 2018-05-17 local                           
#>  glue         1.2.0      2017-10-29 CRAN (R 3.5.0)                  
#>  graphics   * 3.5.0      2018-04-24 local                           
#>  grDevices  * 3.5.0      2018-04-24 local                           
#>  grid         3.5.0      2018-04-24 local                           
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                  
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                  
#>  httr         1.3.1      2017-08-20 CRAN (R 3.5.0)                  
#>  knitr        1.20       2018-02-20 CRAN (R 3.5.0)                  
#>  labeling     0.3        2014-08-23 CRAN (R 3.5.0)                  
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                  
#>  magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                  
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                  
#>  methods    * 3.5.0      2018-04-24 local                           
#>  mime         0.5        2016-07-07 CRAN (R 3.5.0)                  
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.5.0)                  
#>  pillar       1.2.1      2018-02-27 CRAN (R 3.5.0)                  
#>  pkgconfig    2.0.1      2017-03-21 CRAN (R 3.5.0)                  
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                  
#>  R6           2.2.2      2017-06-17 CRAN (R 3.5.0)                  
#>  Rcpp         0.12.16    2018-03-13 CRAN (R 3.5.0)                  
#>  rlang        0.2.0.9001 2018-05-15 Github (r-lib/rlang@854174a)    
#>  rmarkdown    1.9        2018-03-01 CRAN (R 3.5.0)                  
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.5.0)                  
#>  scales       0.5.0.9000 2018-05-15 Github (hadley/scales@d767915)  
#>  stats      * 3.5.0      2018-04-24 local                           
#>  stringi      1.1.7      2018-03-12 CRAN (R 3.5.0)                  
#>  stringr      1.3.0      2018-02-19 CRAN (R 3.5.0)                  
#>  tibble     * 1.4.2      2018-01-22 CRAN (R 3.5.0)                  
#>  tools        3.5.0      2018-04-24 local                           
#>  utils      * 3.5.0      2018-04-24 local                           
#>  withr        2.1.2      2018-05-15 Github (jimhester/withr@79d7b0d)
#>  xml2         1.2.0      2018-01-24 CRAN (R 3.5.0)                  
#>  yaml         2.1.18     2018-03-08 CRAN (R 3.5.0)
@lock

This comment has been minimized.

lock bot commented Nov 16, 2018

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/

@lock lock bot locked and limited conversation to collaborators Nov 16, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.