Non-linear relation of sec.axis to the primary axis

Viewed 42

I am trying to transform the secondary axis of a ggplot using the formula 1/10^x but I am not succeeding. Can someone help?

This is the reproducible code:

x <- as.factor(c(1,2,3,1,2,3,1,2,3,1,2,3))
y <- c(0.0541, 0.0706, 0.4803, 0.0151, 0.0729, 0.0689, 0.0986, 0.1949, 0.3323, 0.5166, 0.0988, 0.2321)
grupo <- as.factor(c(1,1,1,2,2,2,3,3,3,4,4,4))
br <- c(0,0.1, 0.2,0.3,0.4, 0.5, 0.6, 0.7)

df <- data.frame(x =x ,y = y, gr = grupo)

ggplot() +
  geom_point(data=df, aes(x=x, y=y, group=gr, color= gr, shape= gr))+
  geom_line(data=df, aes(x=x, y=y, group=gr, color= gr ))+
  scale_y_reverse(limits = c(0.7, -0.05), breaks = br,
              sec.axis = sec_axis(trans = ~.*10, breaks = br*10))

But I would like to show in the secondary axis the following numbers 1/10^br, that you can see in the image:

the plot

Thank you!!

1 Answers
br <- seq(0, 1, 0.1)

ggplot() +
  geom_point(data=df, aes(x=x, y=y, group=gr, color= gr, shape= gr))+
  geom_line(data=df, aes(x=x, y=y, group=gr, color= gr ))+
  scale_y_reverse(limits = c(0.7, -0.05), breaks = br,
                  sec.axis = sec_axis(trans = ~1/(10^.), breaks = br))

enter image description here


Verification of values:

t <- data.frame(x = seq(0, 0.7, 0.1))
t$pow_ten = 10^t$x
t$sec_ax = 1/t$pow_ten
t

    x  pow_ten    sec_ax
1 0.0 1.000000 1.0000000
2 0.1 1.258925 0.7943282
3 0.2 1.584893 0.6309573
4 0.3 1.995262 0.5011872
5 0.4 2.511886 0.3981072
6 0.5 3.162278 0.3162278
7 0.6 3.981072 0.2511886
8 0.7 5.011872 0.1995262
Related