0

I would like to have two axis using different scales/units, both matching one single dataset. The problem is that the two axis are non-monotonic. An example is shown below:

library(ggplot2)
d <- data.frame(
  time = c(1:10),
  signal1 = seq(10, 100, by=10),
  signal2 = c(10,11,13,14.2, 14.5, 16, 18, 18.2,19.6, 20)
)

axis.transform <- approxfun(d$signal1, d$signal2)

ggplot(d, aes(x=time, y=signal1)) + geom_point() +

scale_y_continuous(
  "signal 1", 
  sec.axis = sec_axis(~axis.transform(.), name = "signal 2")
)
JohnBee
  • 1,720
  • 1
  • 15
  • 19
  • 2
    Possible duplicate of [Plot with 2 y axes, one y axis on the left, and another y axis on the right](https://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y-axis-on-the-right) – pogibas Oct 30 '18 at 10:27
  • 1
    And what is the issue with the solution you found? – s_baldur Oct 30 '18 at 10:35
  • 1
    I clarified the question. The issue is that this solution only works with monotonic functions, which is not my case. – JohnBee Oct 30 '18 at 10:43
  • 1
    @JohnBee then the example graph is not a good example... – s_baldur Oct 30 '18 at 10:49
  • @snoram you are right. I'm reformulating the question with a more representative example – JohnBee Oct 30 '18 at 10:50
  • I'm not sure that understand your idea behind the code completely. But wouldn't be helpful to use `scales::rescale()` instead of `approxfun()` ? – Ekatef Oct 30 '18 at 13:32

0 Answers0