ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Missing ticks and gridlines with reciprocal transform

Open andrewgustar opened this issue 2 years ago • 10 comments

When using the reciprocal transform, no tick marks or breaks or grid lines appear on the relevant axis.

This is with ggplot2 3.4.2 using R 4.3.0 on windows 11, but has also been reported on stackoverflow with other configurations... https://stackoverflow.com/questions/76270211/loosing-grid-lines-and-ticks-upon-reciprocal-transform-in-ggplot2.

Update - this is probably the same issue as https://github.com/tidyverse/ggplot2/issues/5271, fixed in the development version

ggplot(cars, aes (x = speed, y = dist)) + 
  geom_point() +
  scale_y_continuous(trans = 'reciprocal')

andrewgustar avatar May 17 '23 09:05 andrewgustar

duplicate of https://github.com/tidyverse/ggplot2/issues/5271

smouksassi avatar May 17 '23 11:05 smouksassi

Indeed, this is fixed in the dev version already.

teunbrand avatar May 18 '23 04:05 teunbrand

@teunbrand I still have this issue using ggplot2_3.4.2.9000. Is there a newer dev version and if so how can I install it? Cheers, mce

mce13 avatar Jul 21 '23 21:07 mce13

The automatic break calculations are horrible for this transformation, but that is more of an issue in {scales}. If you provide breaks manually it should work as intended.

library(ggplot2)
utils::packageVersion("ggplot2")
#> [1] '3.4.2.9000'

ggplot(cars, aes (x = speed, y = dist)) + 
  geom_point() +
  scale_y_continuous(trans = 'reciprocal',
                     breaks = c(2, 5, 10, 30, 120))

Created on 2023-07-22 with reprex v2.0.2

teunbrand avatar Jul 22 '23 08:07 teunbrand

@teunbrand Hi there, just went back to this today, because I really need to make these plots now for a paper, and I still don't see any ticks or gridlines with your minimal example above. I tried versions 3.4.3, 3.4.4 and 3.4.4.9000. Any suggestions? Should we not reopen this issue, because if not this might never be fixed. Cheers, mce

mce13 avatar Dec 13 '23 09:12 mce13

The example I showed above works with the development version of ggplot2 (3.4.4.9000). Could you show what happens if you run that example?

teunbrand avatar Dec 13 '23 09:12 teunbrand

@teunbrand Basically the same as your plot just without the gridlines and tickmarks on the y-axis (see attached). test

mce13 avatar Dec 13 '23 09:12 mce13

Does the plot render without any warning messages? Do you have the recent scales package (1.3.0) installed? Does running the following code give you the same numbers for the break information?

library(ggplot2)

p <- ggplot(cars, aes (x = speed, y = dist)) + 
  geom_point() +
  scale_y_continuous(trans = 'reciprocal',
                     breaks = c(2, 5, 10, 30, 120))

sc <- layer_scales(p)$y
sc$break_info()
#> $range
#> [1] 0.008333333 0.500000000
#> 
#> $labels
#> [1] "2"   "5"   "10"  "30"  "120"
#> 
#> $major
#> [1] 1.00000000 0.38983051 0.18644068 0.05084746 0.00000000
#> 
#> $minor
#> [1] 1.00000000 0.69491525 0.38983051 0.28813559 0.18644068 0.11864407 0.05084746
#> [8] 0.02542373 0.00000000
#> 
#> $major_source
#> [1] 0.500000000 0.200000000 0.100000000 0.033333333 0.008333333
#> 
#> $minor_source
#> [1] 0.500000000 0.350000000 0.200000000 0.150000000 0.100000000 0.066666667
#> [7] 0.033333333 0.020833333 0.008333333

Created on 2023-12-13 with reprex v2.0.2

teunbrand avatar Dec 13 '23 10:12 teunbrand

@teunbrand First of all thanks a lot for taking a look at this. And: No warnings, version of scales is 1.3.0.9000 and the break info is exactly the same as for you.

mce13 avatar Dec 13 '23 11:12 mce13

Weird, I'll reopen this for now. The fact that the break information is the same suggests to me that the transformation and scale is fine. Because we miss the gridlines as well, it seems like the axis guide is an unlikely suspect. That leaves the view scales intermediary between scales and guides as a suspect, but I can't really investigate as I'm unable to reproduce the issue.

teunbrand avatar Dec 13 '23 12:12 teunbrand

Not sure if you're still not able to reproduce, but I just ran into this same thing in ggplot 3.5 today. Weirdly, I noticed the behavior depends on the choice of limits.

Below, the x axis ranges from 1 to {1/5, 1/10, 1/20, 1/30}: plot-5 plot-10 plot-20 plot-30

Full reprex (edit: missed that you wanted sc$break_info():

library(ggplot2)
utils::packageVersion('ggplot2')
#> [1] '3.5.0'

make_plot <- function(denom) {
  p <- data.frame(x = c(1, 1/denom), y = c(0,0)) |> 
    ggplot(aes(x, y)) +
    scale_x_continuous(transform = 'reciprocal')
  
  sc = layer_scales(p)$y
  print(paste0('=== 1/', denom, ' ==='))
  print(sc$break_info())
  
  ggsave(paste0('~/Desktop/plot-', denom, '.png'), p, width = 5, height = 3)
}

purrr::walk(c(5, 10, 20, 30), make_plot)
#> [1] "=== 1/5 ==="
#> $range
#> [1] 0 0
#> 
#> $labels
#> [1] "0"
#> 
#> $major
#> [1] 0.5
#> 
#> $minor
#> NULL
#> 
#> $major_source
#> [1] 0
#> 
#> $minor_source
#> NULL
#> [1] "=== 1/10 ==="
#> $range
#> [1] 0 0
#> 
#> $labels
#> [1] "0"
#> 
#> $major
#> [1] 0.5
#> 
#> $minor
#> NULL
#> 
#> $major_source
#> [1] 0
#> 
#> $minor_source
#> NULL
#> [1] "=== 1/20 ==="
#> $range
#> [1] 0 0
#> 
#> $labels
#> [1] "0"
#> 
#> $major
#> [1] 0.5
#> 
#> $minor
#> NULL
#> 
#> $major_source
#> [1] 0
#> 
#> $minor_source
#> NULL
#> [1] "=== 1/30 ==="
#> $range
#> [1] 0 0
#> 
#> $labels
#> [1] "0"
#> 
#> $major
#> [1] 0.5
#> 
#> $minor
#> NULL
#> 
#> $major_source
#> [1] 0
#> 
#> $minor_source
#> NULL

Created on 2024-03-08 with reprex v2.1.0

PlethoraChutney avatar Mar 08 '24 23:03 PlethoraChutney

Just to separate a few things; if the default breaks computation yields invalid or unwieldy breaks, that is a {scales} issue. For example, for denom = 30 you could choose your own breaks like so:

library(ggplot2)

denom <- 30

data.frame(x = c(1, 1/denom), y = c(0,0)) |> 
  ggplot(aes(x, y)) +
  scale_x_continuous(
    transform = 'reciprocal',
    breaks = c(30, 1, 0.1, 0.05)
  )

Created on 2024-04-22 with reprex v2.1.0

If the breaks don't display even though they are appropriate, that is a {ggplot2} issue. I still haven't been able to replicate the not rendering of breaks issue, so I'm going to close this again until it can be shown how we can reproduce the issue.

teunbrand avatar Apr 22 '24 07:04 teunbrand