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

conflict of coord_flip() with annotation_logticks() #3954

Closed
DSLituiev opened this issue Apr 20, 2020 · 3 comments · Fixed by #4431
Closed

conflict of coord_flip() with annotation_logticks() #3954

DSLituiev opened this issue Apr 20, 2020 · 3 comments · Fixed by #4431
Labels
bug an unexpected problem or unintended behavior coord 🗺️ layers 📈
Milestone

Comments

@DSLituiev
Copy link

This behaves exactly like the closed issue #881, still here in ggplot2 version 3.3.0

Using a bit modified example

a <- ggplot(msleep, aes(bodywt, brainwt)) +
 geom_point(na.rm = TRUE) +
 scale_x_log10(
   breaks = scales::trans_breaks("log10", function(x) 10^x),
   labels = scales::trans_format("log10", scales::math_format(10^.x))
 ) +
 scale_y_log10(
   breaks = scales::trans_breaks("log10", function(x) 10^x),
   labels = scales::trans_format("log10", scales::math_format(10^.x))
 )

This works:

a + coord_flip()

And this works:

a + annotation_logticks()

Now,

a + coord_flip() + annotation_logticks()

returns error: Error in unit(xticks$x, "native") : 'x' and 'units' must have length > 0

@clauswilke
Copy link
Member

I can confirm this problem.

library(ggplot2)

a <- ggplot(msleep, aes(bodywt, brainwt)) +
  geom_point(na.rm = TRUE) +
  scale_x_log10(
    breaks = scales::trans_breaks("log10", function(x) 10^x),
    labels = scales::trans_format("log10", scales::math_format(10^.x))
  ) +
  scale_y_log10(
    breaks = scales::trans_breaks("log10", function(x) 10^x),
    labels = scales::trans_format("log10", scales::math_format(10^.x))
  )

a + annotation_logticks()

a + annotation_logticks() + coord_flip()
#> Error in unit(xticks$x, "native"): 'x' and 'units' must have length > 0

Created on 2020-04-19 by the reprex package (v0.3.0)

It arises because annotation_logticks() doesn't properly use the coord to figure out where to draw ticks. It just assumes that the x axis is bottom/top and the y axis is left/right. See e.g.:

if (grepl("[b|t]", sides)) {
# Get positions of x tick marks
xticks <- calc_logticks(
base = base,
minpow = floor(panel_params$x.range[1]),
maxpow = ceiling(panel_params$x.range[2]),

@clauswilke
Copy link
Member

The correct way to obtain the ranges would probably be something like this (from geom_hline()):

ggplot2/R/geom-hline.r

Lines 48 to 51 in c7f9398

ranges <- coord$backtransform_range(panel_params)
data$x <- ranges$x[1]
data$xend <- ranges$x[2]

@clauswilke
Copy link
Member

Or just coord$range(panel_params), not sure off the top of my head.

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior coord 🗺️ layers 📈 labels Apr 30, 2020
@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior coord 🗺️ layers 📈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants