Skip to content

sec.axis with scale_*_log10 #1992

Closed
Closed

Description

@pnolain

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?

Activity

  1. hadley commented on Jan 25, 2017

    @hadley
    Member

    @thomasp85 can you take a look?

  2. added
    feature
    on Jan 25, 2017
  3. self-assigned this
    on Feb 28, 2017
  4. thomasp85 commented on Apr 3, 2017

    @thomasp85
    Member

    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

  5. hadley commented on Apr 3, 2017

    @hadley
    Member

    Maybe we could optionally allow supplying the inverse?

  6. thomasp85 commented on Apr 3, 2017

    @thomasp85
    Member

    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🙂

  7. thomasp85 commented on Apr 3, 2017

    @thomasp85
    Member

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

  8. added a commit that references this issue on Oct 30, 2017
    4825054
  9. amignan commented on Dec 11, 2017

    @amignan

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

  10. jacobgrasmick commented on May 20, 2018

    @jacobgrasmick

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

  11. clauswilke commented on May 20, 2018

    @clauswilke
    Member

    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)
  12. lock commented on Nov 16, 2018

    @lock

    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/

  13. locked and limited conversation to collaborators on Nov 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featurescales 🐍

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions