Sec.axis on a log10 scale uses non-transformed breaks

I want to use a log10 y scale for dollar amounts, and a second axis for € conversion, but I can't figure out how to make the second axis have the "nice" breaks automatically used on the original axis. I thought dup_axis and the derive() helper would do the trick, but apparently not.

library(ggplot2)

df <- data.frame(x = rep(c("A", "B", "C"), 100),
                 y = sample(c(20, 20e6, 1e6, 50, 100), 300, replace = TRUE))

ggplot(data = df, aes(x, y)) +
  geom_boxplot() +
  scale_y_log10(labels = scales::dollar, sec.axis = sec_axis(~.*0.85))


ggplot(data = df, aes(x, y)) +
  geom_boxplot() +
  scale_y_log10(labels = scales::dollar, sec.axis = dup_axis(~.*0.85))


packageVersion("ggplot2")
#> [1] '2.2.1.9000'

Created on 2018-07-03 by the reprex
package
(v0.2.0).

Edit:
I have since tested this with ggplot2 v3.0.0 from CRAN, same result.

This is a crosspost of this issue I have not received any feedback for and felt like it might be a good idea to talk about it here, maybe?

No solution yet — I've been kind of hacking away on this one trying to figure it out (I'm super confused by the fact that they don't line up even without the scales::dollars() transformation.
But one change it occurred to me that might be making a difference is that tidy evaluation tools are needed to compute on the mapping of existing ggplot2 objects (Kara Woo described this in an earlier iteration of the release notes here).

Could be totally unrelated, just thought I'd put it out there. I'll keep on plugging away, so please let me know if you figure it out!

library(ggplot2)

df <- data.frame(x = rep(c("A", "B", "C"), 100),
                 y = sample(c(20, 20e6, 1e6, 50, 100), 300, replace = TRUE))

ggplot(data = df, aes(x, y)) +
  geom_boxplot() +
  scale_y_log10(sec.axis = sec_axis(~.*0.85))

Another weird, incorrect, but kinda sorta maybe getting there result — have you tried doing the log10 transformation inside of scale_y_continuous()

library(ggplot2)

df <- data.frame(x = rep(c("A", "B", "C"), 100),
                 y = sample(c(20, 20e6, 1e6, 50, 100), 300, replace = TRUE))

ggplot(data = df, aes(x, y)) +
  geom_boxplot() +
  scale_y_continuous(labels = scales::dollar, trans = "log10", sec.axis = dup_axis(trans = (~(log10(.*0.85))), labels(derive)))

Created on 2018-07-09 by the reprex package (v0.2.0).

The only thing I've done with this so far is to manually fix it using breaks, which is not nice, but it works ¯\_(ツ)_/¯

library(ggplot2)
library(scales)

df <- data.frame(x = rep(c("A", "B", "C"), 100),
                 y = sample(c(20, 20e6, 1e6, 50, 100), 300, replace = TRUE))

ggplot(data = df, aes(x, y)) +
  geom_boxplot() +
  scale_y_log10(labels = scales::dollar, 
                sec.axis = sec_axis(~.*0.85, breaks = 10^(seq(3, 7, 2)),
                                    labels = unit_format("€", sep = "")))

Created on 2018-07-09 by the reprex
package
(v0.2.0).

1 Like

This is now fixed in the dev version of ggplot2!

3 Likes